Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:48:30+00:00 2026-06-16T17:48:30+00:00

In a written test I came across a question which reads as follows: We

  • 0

In a written test I came across a question which reads as follows:

We are given an integer linked list of which both first half and second half are sorted independently. Write a function to merge the two parts to create one single sorted linked list.

Constraint: do not use any extra space.

Test Cases and output:
Input List1 :
4->5->6->7->1->2->3;
Output :
1->2->3->4->5->6->7

Input 2:
5->6->7->8->1->2->3->4;
Output 2 :
1->2->3->4->5->6->7->8

What I can think of is by using two pointers: one for the first half traversal and one for the second half traversal. Using them I can traverse from head to middle (using 1st pointer) and from middle to last (using 2nd Pointer). While traversing both parts simultaneously, I can compare values and swap when needed.

But this solution employs use of two pointers which consumes memory.

Can it be done without using any memory?

As it was a written test, I cannot ask for clarifications.
Assistance is appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:48:31+00:00Added an answer on June 16, 2026 at 5:48 pm

    When they say “do not use extra space”, they do not mean pointers and scalars; they do mean “arrays” and “dynamically allocated structures”. In your case, the amount of memory is fixed.

    Merging two ordered lists is simple: first, cut the list in half, and then re-arrange next pointers of its elements to make the list sorted.

    You will need three pointers – newHead, head1, and head2.

    • Initialize head1 and head2 to the head of the original list
    • Advance head2 until you see a break in the sorted sequence (i.e. when head2->next->value is less than head2->value). Cut the list there by setting head2->next to NULL; keep the original head2->next – it is your new head2

    At this point, you have two independently ordered, separate linked lists, and you can apply the classic merge algorithm. Set newHead to the smaller element of head1 or head2, and then move in a loop, setting the next pointer of the current last element to the smaller of head1 or head2. Once you hit head1->next == NULL or head2->next == NULL, assign the head of the other list to the next of the list that ran out of elements first. You are done – newHead now points to the beginning of a sorted list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

are most of the JUnit test written for public methods or private methods? Which
I have written a JUnit test for a private function which is returning String.
I came across some PHP code that was written by a co-worker (it was
I have written a test, which I know is wrong. I know that this
I came across the following javascript: <body> <script type=text/javascript> (function() { alert('test'); })(); </script>
I have written some test scripts in python which call some apis for a
This is one of the programming questions asked during written test from Microsoft. I
I have some unit tests I've written to test my Django application. One test
This is a simple script I have written to test command line argument handling:
I have written a ScalaCheck test case within Specs2. The test case gives up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.