## Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head nodes of two sorted linked lists. The data in both lists will be sorted in ascending order. Change the next pointers to obtain a single, merged linked list which also has data in ascending order. Either head pointer given may be null meaning that the corresponding list is empty.

## Source code:

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)`/* Merge two sorted lists A and B as one linked list Node is defined as struct Node { int data; struct Node *next; } */ Node* MergeLists(Node *headA, Node* headB) { // This is a "method-only" submission. // You only need to complete this method Node *head=NULL, *cur,*prev = NULL; Node *curA,*curB; head = headA; cur = head; curB = headB; while(curB !=NULL){ if(cur== NULL){ head = headB; break; }else{ if(cur->data > curB->data){ curA = curB; curB = curB->next; curA->next = cur; if(prev == NULL){ head = curA; }else{ prev->next = curA; } prev = cur; cur = cur->next; }else{ if(cur->next !=NULL){ prev = cur; cur = cur->next; }else{ cur->next = curB; break; } }} }return head; }`

thanks for the article. This info on data room might be of great interest for you and your followers as it is quite your sphere of interest.

ReplyDeletewrite code in a way others could understand if it's purpose is to teach not contests.

ReplyDelete