Skip to content

Commit 6580b0d

Browse files
added code for swapping node positoin in linkedlist
1 parent 70bd0c5 commit 6580b0d

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'''
2+
class Node:
3+
def __init__(self):
4+
self.data = None
5+
self.next = None
6+
self.prev = None
7+
'''
8+
def swapNodes(head, k):
9+
temp = head
10+
while temp.next is not None:
11+
temp = temp.next
12+
tail = temp
13+
start = head
14+
end = tail
15+
while k > 1:
16+
start = start.next
17+
end = end.prev
18+
k -= 1
19+
start.data, end.data = end.data, start.data

0 commit comments

Comments
 (0)