We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Linked Lists
  4. Merge two sorted linked lists

Merge two sorted linked lists

Problem
Submissions
Leaderboard
Discussions
Editorial

This challenge is part of a tutorial track by MyCodeSchool

Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty.

Example
refers to
refers to

The new list is

Function Description

Complete the mergeLists function in the editor below.

mergeLists has the following parameters:

  • SinglyLinkedListNode pointer headA: a reference to the head of a list
  • SinglyLinkedListNode pointer headB: a reference to the head of a list

Returns

  • SinglyLinkedListNode pointer: a reference to the head of the merged list

Input Format

The first line contains an integer , the number of test cases.

The format for each test case is as follows:

The first line contains an integer , the length of the first linked list.
The next lines contain an integer each, the elements of the linked list.
The next line contains an integer , the length of the second linked list.
The next lines contain an integer each, the elements of the second linked list.

Constraints

  • , where is the element of the list.

Sample Input

1
3
1
2
3
2
3
4

Sample Output

1 2 3 3 4 

Explanation

The first linked list is:

The second linked list is:

Hence, the merged linked list is:

Author

harsha_s

Difficulty

Easy

Max Score

5

Submitted By

164963

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy