-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_linked_list.py
43 lines (34 loc) · 1000 Bytes
/
reverse_linked_list.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
# https://leetcode.com/problems/reverse-nodes-in-k-group/submissions/
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseKGroup(self, head: 'ListNode', k: 'int') -> 'ListNode':
if not head:
return None
# Check if list is of size k
ptr = head
k_counter = 0
reached_len = False
while ptr and (not reached_len):
ptr = ptr.next
k_counter += 1
if k_counter == k:
reached_len = True
if not reached_len:
return head
# Reverse list
prev = None
curr = head
next = None
k_counter = 0
while curr and k_counter < k:
next = curr.next
curr.next = prev
prev = curr
curr = next
k_counter += 1
head.next = self.reverseKGroup(curr, k)
return prev