-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswap nodes in linkedlist.py
53 lines (47 loc) · 1.26 KB
/
swap nodes in linkedlist.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
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp is not None:
print(temp.data)
temp = temp.next
def swap_node(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 or not curr_2:
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
if __name__ == '__main__':
list1 = LinkedList()
list1.head = Node("mon")
l2 = Node("tue")
l3 = Node("wed")
list1.head.next = l2
l2.next = l3
list1.printList()
list1.swap_node("mon", "tue")
print(" After swapping nodes: ")
list1.printList()