Created
December 16, 2019 19:24
-
-
Save rahul4coding/4670a28083b404de4f4d543357546547 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