Back to course home
0% completed
Vote For New Content
Clean solution
Mohammed Dh Abbas
Jul 27, 2024
class Solution: def reverse(self, head, k): node = head new_head = None prev_end = None # while we still have remaining parts. while node: end = node prev = None count = 0 # reverse the part. while node and count < k: count += 1 next_node = node.next node.next = prev prev = node node = next_node begin = prev # link the previous part end to the begin of the new part. if prev_end: prev_end.next = begin prev_end = end # set the new head of Linkedlist. it points to the first part begin. if not new_head: new_head = begin count = 0 # We have to do this to prevent the Linkedlist from having circular loop at the end. end.next = None return new_head
0
0
Comments
Comments