Interview Bootcamp
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My solution

Mohammed Dh Abbas

Jul 26, 2024

#class Node: #  def __init__(self, value, next=None): #    self.val = value #    self.next = next class Solution:   def reverse(self, head, p, q):     org_head = head     # find the p node     node = head     prev_p = None     count = 1     while count < p:       count += 1       prev_p = node       node = node.next     # reverse from p node to the q node     prev_q = None     count = q     before_reverse = node     while count >= p:       count -= 1       next_node = node.next       node.next = prev_q       prev_q = node       node = next_node         # linking the nodes of the reversed part to the rest of linked list     if prev_p:       prev_p.next = prev_q     else:       org_head = prev_q # edge case if p = 1 = head node     if before_reverse:       before_reverse.next = node     return org_head

0

0

Comments
Comments