Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Renat Zamaletdinov
Similar solution, but with dummy sentinel node, might be a bit easier to understand

Renat Zamaletdinov

Feb 12, 2024

#class Node: #  def __init__(self, value, next=None): #    self.val = value #    self.next = next class Solution:   def reverse(self, head, k):     if k == 1:       return head     def sub_reverse(s, e):       prev = None       curr = s       while curr != e:         next_ = curr.next         curr.next = prev         prev = curr         curr = next_       return prev, start     current = dummy = Node(-1, head)     start = end = head     while end:       count = 0             while end and count < k:         end = end.next         count += 1       new_start, new_end = sub_reverse(start, end if count == k else None)       current.next = new_start       current = new_end       start = end     return dummy.next

1

0

Comments
Comments