Linked Lists · Coding problem

Merge Two Sorted Lists

A free coding problem from the Linked List Merge Technique skill. Read it here, then solve it in the app in Java or Python.

  • Easy
  • Coding problem
  • Free
  • Java and Python

The problem

Combine two sorted linked lists into one sorted list and return its head. Reuse the existing nodes by relinking them, rather than creating new ones.

Input format

Line 1: n1. Line 2: n1 values (omitted if n1 = 0). Line 3: n2. Line 4: n2 values (omitted if n2 = 0).

Example

Input:
4
-2 3 3 9
3
0 3 10
Output:
-2 0 3 3 3 9 10

Constraints

  • 0 <= n1, n2 <= 50
  • -100 <= node value <= 100
  • Both input lists are sorted ascending

How it's graded

  • Run checks your code against the examples. Submit also runs a hidden suite in four groups - basic cases, edge cases, random inputs and performance tests at the size limit - and shows which groups pass, never the hidden inputs.
  • Stuck? Hints come in four steps, from a nudge to a code skeleton, each costing part of the points.
  • Once you pass, you see the approaches from brute force to optimal, and where your solution lands among them.

More problems in Linked List Merge Technique

Solve it Merge Two Sorted Lists free

No card needed. Lessons, practice questions and easy problems are free forever.

Free plan, no card · 7-day Pro trial · Cancel anytime