/
Reverse in group of K.py
65 lines (54 loc) · 1.29 KB
/
Reverse in group of K.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
class Node:
def __init__(self, x):
self.data = x
self.next = None
def insert(head, val):
temp = Node(val)
if head is None:
temp.next = head
return temp
curr = head
while curr.next is not None:
curr = curr.next
curr.next = temp
temp.next = None
return head
def reverse(head, k):
curr = head
prevFirst = None
isFirstPass = True
while curr is not None:
first = curr
prev = None
count = 0
while curr is not None and count < k:
nextNode = curr.next
curr.next = prev
prev = curr
curr = nextNode
count += 1
if isFirstPass:
head = prev
isFirstPass = False
else:
prevFirst.next = prev
prevFirst = first
return head
def printList(head):
curr = head
print("\n\n")
while curr is not None:
print(curr.data, "->", end=" ")
curr = curr.next
print("NULL\n\n")
if __name__ == "__main__":
head = None
n = int(input("Enter n: "))
k = int(input("Enter k: "))
print("Enter nodes:")
for i in range(n):
val = int(input())
head = insert(head, val)
printList(head)
head = reverse(head, k)
printList(head)