⌘K
Flycodx

Merge Two Sorted Lists

Merge Two Sorted Lists

EasyLinked Lists100% accepted

You are given the head nodes of two sorted linked lists. Merge them into a single sorted linked list. The merged list should be made by splicing together the nodes of the two input lists. Your solve(...) function receives these inputs directly as ready-made arguments — no input reading or parsing needed. Just compute and return the answer. Example: list1=[1 2 4], list2=[1 3 4] → "1 1 2 3 4 4"

Input format — only if your code reads from stdin

Input format (stdin): - First line: n space-separated integers (first sorted list; empty line means empty list) - Second line: m space-separated integers (second sorted list) Output: the merged sorted list values, space-separated. Print "empty" if both lists are empty.

Approach

Use a dummy head node. Compare the current heads of both lists; append the smaller node to the result and advance that pointer. When one list is exhausted, attach the remainder of the other directly.

Input

1 2 4 1 3 4

Output

1 1 2 3 4 4
  • •0 ≤ n, m ≤ 50, -100 ≤ node values ≤ 100
solution.py
Loading editor...
Test 1
Test 2
Test 3

Press Run to test your custom input, or Submit to judge against all test cases.

Sign in to continue

Create a free account to run code, submit solutions and track progress.

  • Free forever — no credit card
  • 10+ languages with live execution
  • Points, streaks & leaderboards
Auto Save OnLn 1, Col 1Spaces: 4UTF-8LFPython 3