forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwapNodes-LinkedList.py
166 lines (141 loc) · 3.48 KB
/
SwapNodes-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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'''
Problem Statement-
Given a linked list, i & j, swap the nodes that are
present at i & j position in the Linked List.
You need to swap the entire nodes, not just the data.
Logical Intuition-
We change the links of the prev
and the next pointers of the nodes
we are swapping.
'''
class Node:
def __init__(self, data):
self.data = data
self.next = None
def inputLL():
'''
Summary Line-:
This method helps to take input for the Linked List.
We use '-1' at the end of our input to indicate the
end of our list.
Returns:
Returns the head of our newly
formed Linked List
'''
print("Enter elements of the Linked List and use -1 to end the list-")
llist = list(int(i) for i in input().split())
head = None
tail = None
for data in llist:
if data != -1:
newNode = Node(data)
if head is None:
head = newNode
tail = newNode
else:
tail.next = newNode
tail = newNode
else:
return head
def lengthLL(head):
'''
Summary Line:
Helps us to get the length
of the Linked List.
Args:
head- Head of our Linked List.
Returns:
Length of our Linked list.
'''
count = 0
while head is not None:
count += 1
head = head.next
return count
def swapNodes(head, m, n):
'''
Summary Line:
This function helps us to swap the
particular nodes of the Linked List.
Args:
head- Head of our Linked List
m- index of the first node being swapped.
n- index of the second node being swapped.
Returns:
Head of the new Linked list with
swapped nodes.
'''
if head is None:
return head
x = m
y = n
m = min(x, y)
n = max(x, y)
i = 0
temp = head
prev1 = None
while(i < m):
if temp is None:
break
prev1 = temp
temp = temp.next
i = i+1
current1 = temp # current1 set at the first node
i = 0
temp = head
prev2 = None
while i < n:
if temp is None:
break
prev2 = temp
temp = temp.next
i = i+1
current2 = temp # current2 set to the other node
temp = current2.next
'''
We iterate over the prev and the next nodes
of our current1 and current2 and manually change
their links to swap the nodes.
'''
if prev1 is not None and prev2 is not None:
prev1.next = current2
prev2.next = current1
current2.next = current1.next
current1.next = temp
if prev1 is None:
prev2.next = current1
head = current2
current2.next = current1.next
current1.next = temp
return head
def printLL(head):
'''
Summary Line:
Prints the data in the nodes
of our Linked list.
Args:
head- Head of our Linked list
'''
while head is not None:
print(head.data, end=" ")
head = head.next
print()
if __name__ == "__main__":
head = inputLL()
print("Enter index of nodes to be swapped-")
i, j = map(int, input().split())
head = swapNodes(head, i, j)
printLL(head)
'''
Sample Input-
Enter elements of the Linked List and use -1 to end the list-
3 4 5 2 6 1 9 -1
Enter index of nodes to be swapped-
3 4
Sample Output-
3 4 5 6 2 1 9
Time complexity- O(n) where n is the
number of nodes in the Linked List.
Space complexity- O(1) because we are only
storing definite number of nodes/ values.
'''