https://leetcode.com/problems/merge-two-sorted-lists/
Recursive
Iterative
newnode pointer is needed;- We need a loop for
(list1 && list2), and conditions for when list1’s value is larger than list2’s value, and vice versa - After exiting the loop, check which list pointer is still valid, then simply append
walker->nextto that list pointer - Remember to assign
walkertohead, then delete the head
Mistakes
ListNode *head = new ListNode();
ListNode *walker = head;
walker = list1;
return head;walkeris first assigned to the newly allocatedListNodeobject. However it is then assigned tolist1; now there is nobody connected tohead!- Instead, do
walker->next=list1orhead->next = walker