Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 9, 2016 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/c09df676896e938d7f6c3819e301545c to your computer and use it in GitHub Desktop.
Save jianminchen/c09df676896e938d7f6c3819e301545c to your computer and use it in GitHub Desktop.
HackerRank - LinkedList - Merge Two sorted Linked List
/*
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