• + 4 comments

    Can someone explain to me how the previous node in A points to the new value of A after it has passed. Let me explain:

    A: 1->3->NULL

    ....^

    B: 2->4->6->NULL

    ....^

    Initially we start at the head of the lists. Since 1 < 2 it becomes

    A: 1->3->NULL

    ..........^

    B: 2->4->6->NULL

    ....^

    Now, I understand that 2 points to 3 and 4 becomes the new head after that block of code, but when does 1 point to 2 since the pointer has already passed it? To me, it looks like 1 and 2 are pointing to 3.

    • + 0 comments

      SinglyLinkedListNode* mergeLists(SinglyLinkedListNode* p, SinglyLinkedListNode* q) { SinglyLinkedListNode* sm, *startm; if(p->data<=q->data) { startm=p; p=p->next; } else { startm=q; q=q->next; } sm=startm; while(p!=NULL&&q!=NULL) { if(p->data<=q->data) { sm->next=p; sm=sm->next; p=p->next; } else { sm->next=q; sm=sm->next; q=q->next; } } if(p==NULL) sm->next=q; else sm->next=p; return startm; }

    • + 0 comments

      cant we do this firrst add all the elements and than sort it???

    • + 0 comments

      Did you get the answer of your question?