Skip to main content

3. Merge Two Sorted Lists

easyAsked at Reddit

Merge two sorted linked lists into one sorted list. Reddit uses this to test pointer hygiene — the same skill needed to merge ranked feed streams (hot + new + rising) without losing or duplicating posts.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Reddit loops.

  • Glassdoor (2026-Q1)Reddit feed-team phone screen.
  • Blind (2025-12)Common ramp-up before harder merge-k-streams question.

Problem

You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into one sorted list by splicing together the nodes of the first two lists. Return the head of the merged linked list.

Constraints

  • The number of nodes in both lists is in the range [0, 50].
  • -100 <= Node.val <= 100
  • Both list1 and list2 are sorted in non-decreasing order.

Examples

Example 1

Input
list1 = [1,2,4], list2 = [1,3,4]
Output
[1,1,2,3,4,4]

Example 2

Input
list1 = [], list2 = []
Output
[]

Example 3

Input
list1 = [], list2 = [0]
Output
[0]

Approaches

1. Collect into array then sort

Walk both lists, push values into an array, sort, rebuild a linked list.

Time
O(n log n)
Space
O(n)
function mergeTwoLists(l1, l2) {
  const vals = [];
  while (l1) { vals.push(l1.val); l1 = l1.next; }
  while (l2) { vals.push(l2.val); l2 = l2.next; }
  vals.sort((a,b) => a - b);
  const dummy = { next: null }; let cur = dummy;
  for (const v of vals) { cur.next = { val: v, next: null }; cur = cur.next; }
  return dummy.next;
}

Tradeoff: Throws away the fact that inputs are sorted. Anti-pattern.

2. Dummy head + two-pointer splice (optimal)

Use a dummy head. Compare list1.val and list2.val, splice the smaller node, advance that pointer. Append the remainder at the end.

Time
O(n + m)
Space
O(1)
function mergeTwoLists(list1, list2) {
  const dummy = { val: 0, next: null };
  let tail = dummy;
  while (list1 && list2) {
    if (list1.val <= list2.val) {
      tail.next = list1;
      list1 = list1.next;
    } else {
      tail.next = list2;
      list2 = list2.next;
    }
    tail = tail.next;
  }
  tail.next = list1 || list2;
  return dummy.next;
}

Tradeoff: O(1) extra space because we re-link existing nodes. Reddit's feed merger does this with heap of size k=3 streams.

Reddit-specific tips

Reddit interviewers grade for the dummy-head pattern — it avoids the special-case 'what if the merged list is empty?' branch. Bonus signal: mention that with k > 2 sorted streams (hot, new, rising), you'd switch to a min-heap of (value, stream_id).

Common mistakes

  • Forgetting to advance the tail pointer after splicing.
  • Not appending the remaining list (one will still have nodes).
  • Allocating new nodes when re-linking would suffice — wastes memory.

Follow-up questions

An interviewer at Reddit may pivot to one of these next:

  • Merge k sorted lists (LC 23) — natural extension via min-heap.
  • Merge two sorted arrays in place (LC 88).
  • What if the lists are doubly-linked?

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why use a dummy head?

It removes the 'is this the first node?' branch. We always have a tail to .next= onto.

Should we use list1.val < list2.val or <= ?

<= preserves stability — equal elements from list1 come first. Matters when ranking ties.

Practice these live with InterviewChamp.AI

Drill Merge Two Sorted Lists and other Reddit interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →