Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
My solution

Mohammed Dh Abbas

Jul 6, 2024

#class Node: # def __init__(self, value, next=None): # self.val = value # self.next = next class Solution: def reorder(self, head): def find_mid(head): slow, fast = head, head while slow and fast: if fast: if fast.next: fast = fast.next.next else: return slow else: return slow slow = slow.next return slow def reverse(node): prev = None while node: next_node = node.next node.next = prev prev = node node = next_node return prev # Find the middle and last points then reverse linked list from the middle to the end start = head mid = find_mid(head) last = reverse(mid) # Construct the new linked list by linking start->last->start->last ... new_head = start while last != mid: next_start = start.next next_last = last.next start.next = last last.next = next_start start = next_start last = next_last return new_head

0

0

Comments
Comments

On this page