Linked List Patterns
2025-04-14 23:56
Find the middle
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# slow ptr will have the middle node
Reverse the list
prev, curr = None, head
while curr:
nxt = curr.next
curr.next = prev
prev = curr
curr = nxt
Merge two array (Cross One)
a = [1,2,3,4]
b = ['a','b','c']
# after merge
new_arr = [1, 'a', 2, 'b', 3, 'c', 4]
# logic
first, second = head1, head2
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first, second = tmp1, tmp2