SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Course Overview
Singly Linked ListReverse Linked ListDoubly Linked ListData-Structure and Linked-List Trade-offsMerge Two Sorted ListsRemove Nth Node From End of ListStackValid ParenthesesMin StackDaily TemperaturesQueueImplement Queue Using StacksCircular QueuesCircular Queues QuizDesign Circular QueueSliding Window MaximumQueues, Circular Buffers, and Monotonic Deques
Linked Lists, Stacks & Queues

Merge Two Sorted Lists

Merge two sorted linked lists by reconnecting their existing nodes.

Try It Yourself

Use the editor below to combine two sorted linked lists.

Merge the existing nodes

Compare the current nodes and attach the smaller one to the merged tail. Either list may be empty, and equal or negative values may appear.

Submit your implementation when it passes the examples.

Merge Two Sorted Lists

easyLinked List · Two Pointers25 minLC 21
Problem

Given the heads of two sorted singly linked lists, combine their existing nodes into one sorted list.

Return the head of the merged list. Each node has the shape { val, next }.

Example 1:

Input: list1 = [1, 2, 4], list2 = [1, 3, 4]
Output: [1, 1, 2, 3, 4, 4]
Explanation: Taking the smaller available node from the two lists places both 1s first, then 2, 3, and both 4s.

Example 2:

Input: list1 = [], list2 = []
Output: []
Explanation: Both lists are empty, so there are no nodes to merge.

Constraints

  • 0 ≤ total number of nodes ≤ 100
  • Both input lists are sorted in non-decreasing order.
0 attempts

Test results

Results appear here after a run.

Solution

Comparing heads 1 and 2 selects node 1 for the merged tail after the dummy.

A dummy node gives the merged list a stable starting point. Repeatedly connect the tail to the smaller current node and advance only that list.

When one list ends, attach the entire remaining chain with a single tail.next assignment. Return dummy.next. The dummy supplied a place for the first assignment and contributes no value to the result.

function mergeTwoLists(list1, list2) {
  const dummy = { val: 0, next: null };
  let tail = dummy;
  let first = list1;
  let second = list2;

  while (first !== null && second !== null) {
    if (first.val <= second.val) {
      tail.next = first;
      first = first.next;
    } else {
      tail.next = second;
      second = second.next;
    }

    tail = tail.next;
  }

  tail.next = first ?? second;
  return dummy.next;
}

Big O notation

MeasureComplexityExplanation
TimeO(n + m)Lists of lengths n and m need at most n + m - 1 comparisons when both are nonempty. The final link attaches the remaining chain.
Auxiliary spaceO(1)Only a dummy node and a fixed number of references are added.

How is this practice?

Previous lessonData-Structure and Linked-List Trade-offsNext lessonRemove Nth Node From End of List
Edit on GitHubReport on GitHub
On this page
%

On this page

Try It YourselfSolutionBig O notation