Merge Two Lists Alternately
Build one list by taking nodes alternately: first from l1, then from l2, then l1 again, and so on. When one list runs out, attach all the rest of the other list. Return the head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
l1 [1,3,5]
l2 [2,4]
returns [1,2,3,4,5]
1, 2, 3, 4, then the leftover 5.
Example 2
l1 []
l2 [7,8]
returns [7,8]
Only the second list has nodes.
Constraints
0 <= nodes in each list <= 50-100 <= Node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.