Skip to content

Commit ebd06d6

Browse files
authored
Merge pull request vJechsmayr#104 from pikachu28/feature/reverseKgroups
Create 0025_Reverse_Node_in_K_Groups.py
2 parents 2ca061c + 9b5a136 commit ebd06d6

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Created on Fri Oct 2 00:13:21 2020
5+
6+
@author: anjalisingh
7+
8+
Leet Code Accepted Code
9+
"""
10+
11+
class Node:
12+
def __init__(self, val=None):
13+
self.val = val;
14+
self.next = None
15+
16+
class Solution:
17+
def __init__(self):
18+
self.head = None
19+
20+
# def InsertAtBeg(self, d):
21+
# nn = Node(d)
22+
# nn.next = self.head
23+
# self.head = nn
24+
25+
26+
27+
# def Print(self):
28+
# printVal = self.head
29+
# while printVal is not None:
30+
# print(printVal.val)
31+
# printVal = printVal.next
32+
33+
def reverseKGroup(self, head, k):
34+
cnt = 0
35+
c =0
36+
curr = head
37+
next = None
38+
prev = None
39+
temp = curr
40+
while temp is not None and c<k :
41+
temp = temp.next
42+
c = c + 1
43+
print(c)
44+
if c == k:
45+
while (curr is not None and cnt < k):
46+
next = curr.next
47+
curr.next = prev
48+
prev = curr
49+
curr = next
50+
cnt = cnt + 1
51+
52+
if next is not None:
53+
head.next = self.reverseKGroup(next, k)
54+
return prev
55+
else:
56+
return curr
57+
58+
# def ReverseNodeK(self, k):
59+
# self.head = self.reverseKGroup(self.head, k)
60+
61+
62+
63+
64+
# LL = Solution()
65+
# LL.InsertAtBeg(5)
66+
# LL.InsertAtBeg(4)
67+
# LL.InsertAtBeg(3)
68+
# LL.InsertAtBeg(2)
69+
# LL.InsertAtBeg(1)
70+
# #LL.Print()
71+
# LL.ReverseNodeK(3)
72+
# LL.Print()

0 commit comments

Comments
 (0)