Created
May 9, 2016 19:04
-
-
Save jianminchen/c09df676896e938d7f6c3819e301545c to your computer and use it in GitHub Desktop.
HackerRank - LinkedList - Merge Two sorted Linked List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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) | |
{ | |
if(headA == NULL && headB == NULL) | |
return NULL; | |
if(headA == NULL) | |
return headB; | |
if(headB == NULL) | |
return headA; | |
if(headA->data <= headB->data) | |
{ | |
headA->next = MergeLists(headA->next, headB); | |
return headA; | |
} | |
else | |
{ | |
headB->next = MergeLists(headA, headB->next); | |
return headB; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment