data_structures.linked_list.swap_nodes¶
Attributes¶
Classes¶
Module Contents¶
- class data_structures.linked_list.swap_nodes.LinkedList¶
- __iter__() collections.abc.Iterator ¶
>>> linked_list = LinkedList() >>> list(linked_list) [] >>> linked_list.push(0) >>> tuple(linked_list) (0,)
- __len__() int ¶
>>> linked_list = LinkedList() >>> len(linked_list) 0 >>> linked_list.push(0) >>> len(linked_list) 1
- push(new_data: Any) None ¶
Add a new node with the given data to the beginning of the Linked List.
- Args:
new_data (Any): The data to be added to the new node.
- Returns:
None
- Examples:
>>> linked_list = LinkedList() >>> linked_list.push(5) >>> linked_list.push(4) >>> linked_list.push(3) >>> linked_list.push(2) >>> linked_list.push(1) >>> list(linked_list) [1, 2, 3, 4, 5]
- swap_nodes(node_data_1: Any, node_data_2: Any) None ¶
Swap the positions of two nodes in the Linked List based on their data values.
- Args:
node_data_1: Data value of the first node to be swapped. node_data_2: Data value of the second node to be swapped.
- Note:
If either of the specified data values isn’t found then, no swapping occurs.
Examples: When both values are present in a linked list.
>>> linked_list = LinkedList() >>> linked_list.push(5) >>> linked_list.push(4) >>> linked_list.push(3) >>> linked_list.push(2) >>> linked_list.push(1) >>> list(linked_list) [1, 2, 3, 4, 5] >>> linked_list.swap_nodes(1, 5) >>> tuple(linked_list) (5, 2, 3, 4, 1)
- When one value is present and the other isn’t in the linked list.
>>> second_list = LinkedList() >>> second_list.push(6) >>> second_list.push(7) >>> second_list.push(8) >>> second_list.push(9) >>> second_list.swap_nodes(1, 6) is None True
- When both values are absent in the linked list.
>>> second_list = LinkedList() >>> second_list.push(10) >>> second_list.push(9) >>> second_list.push(8) >>> second_list.push(7) >>> second_list.swap_nodes(1, 3) is None True
- When linkedlist is empty.
>>> second_list = LinkedList() >>> second_list.swap_nodes(1, 3) is None True
- Returns:
None
- data_structures.linked_list.swap_nodes.linked_list¶