[
linked list
two pointers
]
BinarySearch 0142 Interleaved Linked List
Problem statement
https://binarysearch.com/problems/Interleaved-Linked-List/
Solution
Just use two pointers, choosing one list or another. In the end attach the tail.
Complexity
It is O(n + m)
for time and O(1)
for space.
Code
class Solution:
def solve(self, l0, l1):
head = tail = LLNode(-1)
step = 0
while l0 and l1:
if step%2 == 0:
tail.next = l0
l0 = l0.next
else:
tail.next = l1
l1 = l1.next
tail = tail.next
step += 1
if l1: tail.next = l1
if l0: tail.next = l0
return head.next