forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap_nodes.py
84 lines (65 loc) · 2.34 KB
/
swap_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from typing import Any
class Node:
def __init__(self, data: Any) -> None:
"""
Initialize a new Node with the given data.
Args:
data: The data to be stored in the node.
"""
self.data = data
self.next: Node | None = None # Reference to the next node
class LinkedList:
def __init__(self) -> None:
"""
Initialize an empty Linked List.
"""
self.head: Node | None = None # Reference to the head (first node)
def print_list(self):
"""
Print the elements of the Linked List in order.
"""
temp = self.head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
print()
def push(self, 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.
"""
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
def swap_nodes(self, node_data_1, node_data_2) -> 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.
"""
if node_data_1 == node_data_2:
return
else:
node_1 = self.head
while node_1 is not None and node_1.data != node_data_1:
node_1 = node_1.next
node_2 = self.head
while node_2 is not None and node_2.data != node_data_2:
node_2 = node_2.next
if node_1 is None or node_2 is None:
return
# Swap the data values of the two nodes
node_1.data, node_2.data = node_2.data, node_1.data
if __name__ == "__main__":
ll = LinkedList()
for i in range(5, 0, -1):
ll.push(i)
print("Original Linked List:")
ll.print_list()
ll.swap_nodes(1, 4)
print("After swapping the nodes whose data is 1 and 4:")
ll.print_list()