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

  • Home
  • SEARCH
  • 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 4082748
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:13:33+00:00 2026-05-20T18:13:33+00:00

#define SWAP_PTRS(a, b) do { void *t = (a); (a) = (b); (b) =

  • 0
#define SWAP_PTRS(a, b) do { void *t = (a); (a) = (b); (b) = t; } while (0)

Node* MergeLists(Node* list1, Node* list2) 
{
  Node *list = NULL, **pnext = &list;

  if (list2 == NULL)
    return list1;

  while (list1 != NULL)
  {
    if (list1->data > list2->data)
      SWAP_PTRS(list1, list2);

    *pnext = list1;
    pnext = &list1->next;
    list1 = *pnext;
  }

  *pnext = list2;
  return list;
}

This code is from here, the chosen answer of this question.

I cannot understand 3 lines here:

*pnext = list1;
pnext = &list1->next;
list1 = *pnext;

Can anyone kindly help me? Explain it for me?

Edited: Can I change these 2 lines:

pnext = &list1->next;
list1 = *pnext;

to

 list = list-> next; 
  • 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-05-20T18:13:33+00:00Added an answer on May 20, 2026 at 6:13 pm

    From the start: You have two lists and a new list head which you will be returning. pnext points to that initially.

    The code aims to do as few pointer reassignments as possible, so tries to keep the initial ‘next’ pointers of the input lists intact.

    list1          pnext->list->Null
    |
    V
    o->o->o...
    
    list2
    |
    V
    o->o->o...
    

    Swap is there to ensure that the smaller element is the first of list1. What those lines does is:

    Going step by step:

    *pnext = list1;
    

    Gets *pnext (which is list before the first iteration) to point to the node containing the smallest element:

    list1
    |
    V
    o->o->o...
    ^
    |
    list<-pnext
    
    list2
    |
    V
    o->o->o...
    

    .

    pnext = &list1->next;
    

    Is the tricky part, as noted before & operator has low precedence. It’s also hard to display graphically for it actually looks into part of a Node construct. Something like this though:

    list1
    |
    V
    o---->o->o...
    ^     ^
    |     |
    list  x<-pnext
    
    list2
    |
    V
    o->o->o...
    

    where x is the next pointer of the o which list1 points to.

    list1 = *pnext;
    

    Advances the list1 head, as its first element is processed.

       list1<-pnext
       |
       V
    o->o->o->...
    ^
    |
    list
    
    list2
    |
    V
    o->o->o->...
    

    You have nothing to do with list from here on, for you want to return it as the head of the merged list.

    The invariant there is pnext points to where the last processed element’s next points to, which is where the smallest element from list1-2 should go. The interesting stuff happens with swaps, try to work out the exact proceedings yourself (hard to draw like this, and good exercise to understand what ** does). I might add it if I find a good way to draw it.

    You cannot use list = list-> next; for it would do something like this:

       list1
       |
       V
    o->o->o->...
       ^
       |
       list
    
    list2
    |
    V
    o->o->o->...
    

    Which means you lose that lonely o (and everything else eventually as the loop progresses).

    edit: The *pnext = list2; at the end does this:

    Loop termination (state before the said statement):

             list1<-pnext
             |
             V
    o->o->o->null
    ^
    |
    list
    
          o->o->o->...
          ^
          |
          list2
    

    After the statement:

             list1
             |
             V              
    o->o->o  Null
    ^     |
    |     |
    list  |
          V
          o->o->o->...
          ^
          |
          list2<-pnext
    

    That statement appends the remaining list to the end of the list. Then Node* list is returned, pointing to the head of the merged list.

    edit2:

    And all the way, pnext would be better represented like this:

             list1
             |
             V              
    o->o->o  Null
    ^     |
    |     |<-pnext
    list  |
          V
          o->o->o->...
          ^
          |
          list2
    

    Which means it points to the next pointer of the last processed node.

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

Sidebar

Related Questions

(define a 42) (set! 'a 10) (define a 42) (define (symbol) 'a) (set! (symbol)
How do you define your UserControls as being in a namespace below the project
I want to define something like this in php : $EL = \n<br />\n;
How would you define testing? In the interest of full disclosure, I'm posting this
Can you define a macro that accesses a normal variable, but in a read-only
I try to define a schema for XML documents I receive. The documents look
I'm trying to define a task that emits (using echo) a message when a
In C# we can define a generic type that imposes constraints on the types
Is it possible to define a timestamp column in a MySQL table that will
I need to define a calculated member in MDX (this is SAS OLAP, but

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.