Skip to content

Instantly share code, notes, and snippets.

@Preetiraj3697
Forked from rahul4coding/mergeLists.js
Created October 21, 2022 05:42
Show Gist options
  • Save Preetiraj3697/b79e2f900e3b7b74c1a023365d7265fe to your computer and use it in GitHub Desktop.
Save Preetiraj3697/b79e2f900e3b7b74c1a023365d7265fe to your computer and use it in GitHub Desktop.
merge two sorted Linked list
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;
}
@Preetiraj3697
Copy link
Author

merge two sorted Linked list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment