Linked List Zipper Merge - aloalgo

Linked List Zipper Merge

Easy

You are given two singly linked lists.

You task is to merge into into a single list. The merge should be done in a "zipper" fashion, alternating nodes from each list. The head of the first list should be the head of the merged list.

If one list is longer than the other, the remaining nodes of the longer list should be appended to the end of the merged list.

Example 1

Inputs
list1 = 1 → 2 → 3 → null
list2 = 4 → 5 → 6 → null
Output
1 → 4 → 2 → 5 → 3 → 6 → null
Explanation:

The nodes are taken alternately from each list: 1 from list1, 4 from list2, 2 from list1, 5 from list2, and so on.

Example 2

Inputs
list1 = 1 → 2 → 3 → 4 → null
list2 = 5 → 6 → null
Output
1 → 5 → 2 → 6 → 3 → 4 → null
Explanation:

After list2 is exhausted, the remaining nodes of list1 (3 and 4) are appended to the end.

Example 3

Inputs
list1 = 1 → 2 → null
list2 = 3 → 4 → 5 → 6 → null
Output
1 → 3 → 2 → 4 → 5 → 6 → null
Explanation:

After list1 is exhausted, the remaining nodes of list2 (4, 5, and 6) are appended to the end.

Loading...
Inputs
list1 = 1 → 2 → 3 → null
list2 = 4 → 5 → 6 → null
Output
1 → 4 → 2 → 5 → 3 → 6 → null

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!