Given two sorted linked lists, what is the printed state of the merged linked list?
public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public class Solution { public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(); ListNode current = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } current.next = l1 ?? l2; return dummy.next; } public void PrintList(ListNode head) { while (head != null) { System.Console.Write(head.val); if (head.next != null) System.Console.Write(" -> "); head = head.next; } System.Console.WriteLine(" -> null"); } } // Input lists: // l1: 1 -> 3 -> 5 -> null // l2: 2 -> 4 -> 6 -> null var l1 = new ListNode(1, new ListNode(3, new ListNode(5))); var l2 = new ListNode(2, new ListNode(4, new ListNode(6))); var sol = new Solution(); var merged = sol.MergeTwoLists(l1, l2); sol.PrintList(merged);
Remember to compare the current nodes of both lists and attach the smaller one to the merged list.
The code merges two sorted lists by always choosing the smaller current node from either list. The final merged list is sorted: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null.
Given two sorted linked lists with lengths 4 and 3 respectively, how many nodes will the merged linked list contain?
The merged list contains all nodes from both lists.
The merged list contains all nodes from both lists combined, so total nodes = 4 + 3 = 7.
What error will this code produce when trying to merge two sorted linked lists?
public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public class Solution { public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(); ListNode current = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } current = current.next; } current.next = l1 + l2; return dummy.next; } }
Check the line where two lists are combined using '+'.
The code tries to add two ListNode objects using '+', which is not defined, causing a TypeError.
Given two sorted linked lists, which option merges them to produce the exact output below?
0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null
Check the order of nodes in both lists and how they merge.
Option D merges to 0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null in sorted order.
Examine the merge function below. Why does it cause an infinite loop when merging two sorted linked lists?
public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public class Solution { public ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(); ListNode current = dummy; while (l1 != null && l2 != null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } // Missing current = current.next here } current.next = l1 ?? l2; return dummy.next; } }
Check if the pointer that builds the merged list advances inside the loop.
The current pointer is never moved forward inside the loop, so current.next keeps pointing to the same node, causing an infinite loop.
