Bird
Raised Fist0
DSA C#programming~20 mins

Merge Two Sorted Linked Lists in DSA C# - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Merging Linked Lists
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after merging two sorted linked lists?

Given two sorted linked lists, what is the printed state of the merged linked list?

DSA C#
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);
A2 -> 4 -> 6 -> 1 -> 3 -> 5 -> null
B1 -> 3 -> 5 -> 2 -> 4 -> 6 -> null
C1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
D1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint

Remember to compare the current nodes of both lists and attach the smaller one to the merged list.

🧠 Conceptual
intermediate
1:00remaining
How many nodes are in the merged linked list?

Given two sorted linked lists with lengths 4 and 3 respectively, how many nodes will the merged linked list contain?

A7
B6
C4
D3
Attempts:
2 left
💡 Hint

The merged list contains all nodes from both lists.

❓ Predict Output
advanced
1:30remaining
What error does this code raise when merging two lists?

What error will this code produce when trying to merge two sorted linked lists?

DSA C#
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;
    }
}
ATypeError
BNullReferenceException
CSyntaxError
DNo error, runs correctly
Attempts:
2 left
💡 Hint

Check the line where two lists are combined using '+'.

🚀 Application
advanced
2:00remaining
Which option produces the merged list: 0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null?

Given two sorted linked lists, which option merges them to produce the exact output below?

0 -> 1 -> 1 -> 2 -> 3 -> 4 -> null

Al1: 0 -> 1 -> 1, l2: 2 -> 3 -> 4
Bl1: 0 -> 2 -> 4, l2: 1 -> 1 -> 3
Cl1: 1 -> 2 -> 3, l2: 0 -> 1 -> 4
Dl1: 0 -> 1 -> 3, l2: 1 -> 2 -> 4
Attempts:
2 left
💡 Hint

Check the order of nodes in both lists and how they merge.

🔧 Debug
expert
2:30remaining
Why does this merge function cause an infinite loop?

Examine the merge function below. Why does it cause an infinite loop when merging two sorted linked lists?

DSA C#
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;
    }
}
ABecause l1 and l2 are not updated inside the loop
BBecause current pointer is never moved forward, causing the next pointer to loop back
CBecause dummy node is not initialized properly
DBecause the function returns dummy instead of dummy.next
Attempts:
2 left
💡 Hint

Check if the pointer that builds the merged list advances inside the loop.

Practice

(1/5)
1. What is the main goal when merging two sorted linked lists?
easy
A. Remove duplicates from both lists
B. Create a new sorted linked list containing all elements from both lists
C. Reverse both linked lists
D. Sort each list individually without merging

Solution

  1. Step 1: Understand the problem goal

    The goal is to combine two sorted linked lists into one sorted linked list.
  2. Step 2: Clarify the expected output

    The output should be a single linked list that maintains sorted order and contains all nodes from both input lists.
  3. Final Answer:

    Create a new sorted linked list containing all elements from both lists -> Option B
  4. Quick Check:

    Merge means combine sorted lists into one sorted list [OK]
Hint: Merging means combining sorted lists into one sorted list [OK]
Common Mistakes:
  • Thinking merge means reversing lists
  • Trying to sort lists separately without merging
  • Removing duplicates instead of merging
2. Which of the following is the correct C# syntax to declare a node class for a singly linked list used in merging?
easy
A. class Node { int val; Node next; Node(int x) { val = x; next = null; } }
B. public struct Node { int val; Node next; }
C. public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } }
D. public class Node { int val; int next; public Node(int x) { val = x; next = 0; } }

Solution

  1. Step 1: Check class declaration and access modifiers

    public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } correctly declares a public class with public fields and a constructor initializing values.
  2. Step 2: Verify field types and initialization

    public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } uses 'int val' and 'Node next' with proper initialization; others miss public or use wrong types.
  3. Final Answer:

    public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } -> Option C
  4. Quick Check:

    Correct class with public fields and constructor [OK]
Hint: Node class needs public fields and constructor for linked list [OK]
Common Mistakes:
  • Missing public keyword causing access errors
  • Using struct instead of class for linked list node
  • Incorrect type for next pointer
3. What will be the output linked list after merging these two sorted lists?

List1: 1 -> 3 -> 5 -> null
List2: 2 -> 4 -> 6 -> null

Assuming the merge function merges correctly.
medium
A. 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
B. 1 -> 3 -> 5 -> 2 -> 4 -> 6 -> null
C. 2 -> 4 -> 6 -> 1 -> 3 -> 5 -> null
D. 1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null

Solution

  1. Step 1: Compare first nodes of both lists

    1 (List1) < 2 (List2), so 1 is first in merged list.
  2. Step 2: Continue comparing nodes in order

    Next compare 3 and 2, 2 is smaller, then 3 and 4, 3 is smaller, then 5 and 4, 4 is smaller, then 5 and 6, 5 is smaller, finally 6.
  3. Final Answer:

    1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null -> Option A
  4. Quick Check:

    Merge sorted lists by picking smaller node each step [OK]
Hint: Pick smaller node from heads repeatedly to merge [OK]
Common Mistakes:
  • Appending one list entirely before the other
  • Not maintaining sorted order
  • Mixing node order incorrectly
4. Given the following code snippet for merging two sorted linked lists, what is the error?

public Node Merge(Node l1, Node l2) {
    if (l1 == null) return l2;
    if (l2 == null) return l1;
    if (l1.val < l2.val) {
        l1.next = Merge(l1.next, l2);
        return l1;
    } else {
        l2.next = Merge(l1, l2.next);
        return l2;
    }
}
medium
A. No error; the code correctly merges two sorted linked lists
B. Missing base case for empty lists
C. Incorrect comparison operator used
D. The function does not handle cycles in lists

Solution

  1. Step 1: Analyze base cases

    Code returns the other list if one is null, correctly handling empty lists.
  2. Step 2: Check recursive merge logic

    Code compares values and recursively merges the rest, correctly linking nodes in sorted order.
  3. Final Answer:

    No error; the code correctly merges two sorted linked lists -> Option A
  4. Quick Check:

    Recursive merge with base cases and correct comparisons [OK]
Hint: Check base cases and recursive calls carefully [OK]
Common Mistakes:
  • Forgetting to return merged list
  • Incorrect base case handling
  • Wrong comparison operator
5. You have two sorted linked lists:
List1: 1 -> 4 -> 5 -> null
List2: 1 -> 3 -> 4 -> null

After merging, what is the correct merged list?
Also, explain why duplicates appear and how the merge handles them.
hard
A. 1 -> 1 -> 4 -> 3 -> 4 -> 5 -> null
B. 1 -> 3 -> 4 -> 5 -> null
C. 1 -> 4 -> 5 -> 3 -> 4 -> 1 -> null
D. 1 -> 1 -> 3 -> 4 -> 4 -> 5 -> null

Solution

  1. Step 1: Merge nodes in sorted order including duplicates

    Both lists have '1' and '4' values; merge keeps all nodes, so duplicates appear in order.
  2. Step 2: Explain duplicate handling

    Merge does not remove duplicates; it links nodes as they appear, preserving duplicates in sorted order.
  3. Final Answer:

    1 -> 1 -> 3 -> 4 -> 4 -> 5 -> null -> Option D
  4. Quick Check:

    Duplicates kept; merge preserves all nodes sorted [OK]
Hint: Merge keeps duplicates; just link nodes in sorted order [OK]
Common Mistakes:
  • Removing duplicates mistakenly
  • Mixing node order
  • Appending lists without sorting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard