-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path03_singlyLL_swap_nodes.py
145 lines (112 loc) · 3.29 KB
/
03_singlyLL_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class Node:
def __init__(self, data):
self.data = data
self.next = None
class SinglyLinkedList:
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
return
cur_node = self.head
self.head = new_node
new_node.next = cur_node
def insert_after(self, key, data):
cur = self.head
while cur:
if cur.next is None and cur.data == key:
self.append(data)
return
elif cur.data == key:
new_node = Node(data)
nxt = cur.next
new_node.next = nxt
cur.next = new_node
cur = cur.next
if cur is None:
print("Previous Node is not present in the list")
return
def delete_node(self, key):
# Case 1
cur_node = self.head
if cur_node and cur_node.data == key:
self.head = cur_node.next
cur_node = None
return
# Case 2
prev = None
while cur_node and cur_node.data != key:
prev = cur_node
cur_node = cur_node.next
if cur_node is None:
print("The Node is not present in the list")
return
prev.next = cur_node.next
cur_node = None
def delete_node_at_pos(self, pos):
cur_node = self.head
if pos == 0:
self.head = cur_node.next
cur_node = None
return
prev = None
count = 0
while cur_node and count != pos:
prev = cur_node
count += 1
cur_node = cur_node.next
if cur_node is None:
print("The Node is not present in the list")
return
prev.next = cur_node.next
cur_node = None
def print_list(self):
cur_node = self.head
while cur_node:
print(cur_node.data)
cur_node = cur_node.next
def swap_nodes(self, key_1, key_2):
if key_1 == key_2:
return
prev_1 = None
curr_1 = self.head
while curr_1 and curr_1.data != key_1:
prev_1 = curr_1
curr_1 = curr_1.next
prev_2 = None
curr_2 = self.head
while curr_2 and curr_2.data != key_2:
prev_2 = curr_2
curr_2 = curr_2.next
if not curr_1 and curr_2:
print("No Nodes found")
return
if prev_1:
prev_1.next = curr_2
else:
self.head = curr_2
if prev_2:
prev_2.next = curr_1
else:
self.head = curr_1
curr_1.next, curr_2.next = curr_2.next, curr_1.next
swappingNode = SinglyLinkedList()
swappingNode.append("A")
swappingNode.append("B")
swappingNode.append("C")
swappingNode.append("D")
swappingNode.append("E")
swappingNode.append("F")
swappingNode.swap_nodes("A", "D")
swappingNode.print_list()