-
-
Save Preetiraj3697/b79e2f900e3b7b74c1a023365d7265fe to your computer and use it in GitHub Desktop.
merge two sorted Linked list
This file contains hidden or 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
function mergeLists(head1, head2) { | |
var result = new SinglyLinkedListNode(); | |
// case1 | |
if(head1==null){ | |
return head2; | |
}else if(head2==null){ | |
return head1; | |
} | |
//case 2 | |
if(head1.data <= head2.data){ | |
result = head1; | |
result.next = mergeLists(head1.next,head2) | |
}else{ | |
result = head2; | |
result.next = mergeLists(head1, head2.next) | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
merge two sorted Linked list