Skip to content

Instantly share code, notes, and snippets.

@ionox0
Created March 19, 2014 09:03
Show Gist options
  • Save ionox0/9638039 to your computer and use it in GitHub Desktop.
Save ionox0/9638039 to your computer and use it in GitHub Desktop.
list merge for previously sorted JS linked lists. utilizes my Linked List class
function merge(list1, list2){
var output = new LinkedList();
while(list1 != null | list2 != null){
console.log("asdf");
if (!list2){
output.add(list1.value);
list1 = list1.next;
} else if (!list1){
output.add(list2.value);
list2 = list2.next;
} else if (list1.value < list2.value){
output.add(list1.value);
list1 = list1.next;
} else if (list2.value < list1.value | !list1.value){
output.add(list2.value);
list2 = list2.next;
} else {
output.add(list1.value);
list1 = list1.next;
list2 = list2.next;
}
}
console.log(output);
return output;
}
var list1 = {value:1, next:{value:2, next:{value:4, next:{value:8, next:null}}}};
var list2 = {value:1, next:{value:3, next:{value:6, next:{value:8, next:{value:20, next:null}}}}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment