Created
December 21, 2020 23:54
-
-
Save celestelayne/edf4265c35f06a15c244757fd64e2570 to your computer and use it in GitHub Desktop.
practice problem
This file contains 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
// ------------- | |
// Instructions: | |
// ------------- | |
// | |
// 1 create two linked lists sorted by date | |
// 2. create a new linked list (combined list) | |
// 3. compare head of list-01 with head of list-02 (currentNode = this.head) | |
// 4. if list-01.head < list-02.head | |
// 5. add list-01.head to combined list (using add to tail method) | |
// 6. traversing list-01 | |
// ------------ | |
// Constraints: | |
// ------------ | |
// | |
// 1. Don't mutate the original lists?? | |
// ------------ | |
// Explanation: | |
// ------------ | |
// | |
// list-01 | |
// 1 -> 2 -> 6 -> null | |
// list-02 | |
// 3 -> 5 -> [ 7 -> 9 -> null ] | |
// combined list | |
// 1 -> 2 -> 3 -> 5 -> 6 -> 7 -> 9 -> null | |
function combinedList(listOne, listTwo){ | |
// if both lists are empty | |
let listOneCurrentNode = listOne.head; | |
let listTwoCurrentNode = listTwo.head; | |
if(!listOneCurrentNode){ | |
console.log('list one is empty') | |
} else if(!listTwoCurrentNode){ | |
console.log('list two is empty') | |
} | |
// console.log(listOneCurrentNode.val) | |
// console.log(listTwoCurrentNode.val) | |
const combined = new linkedList(null, null) // error here, no parameters | |
if(listOneCurrentNode.val <= listTwoCurrentNode.val){ | |
combined.addToTail(listOneCurrentNode); | |
listOneCurrentNode = listOneCurrentNode.getNextNode(); // also works with .next | |
} else { | |
combined.addToTail(listTwoCurrentNode); | |
listTwoCurrentNode = listTwoCurrentNode.getNextNode() // also works with .next | |
} | |
while(listOneCurrentNode && listTwoCurrentNode){ | |
if(listOneCurrentNode.val <= listTwoCurrentNode.val){ | |
combined.addToTail(listOneCurrentNode); | |
listOneCurrentNode = listOneCurrentNode.next | |
} else { | |
combined.addToTail(listTwoCurrentNode); | |
listTwoCurrentNode = listTwoCurrentNode.next | |
} | |
} | |
if(listOneCurrentNode == null){ | |
combined.addToTail(listTwoCurrentNode); | |
listTwoCurrentNode = listTwoCurrentNode.next | |
} else if(listTwoCurrentNode == null){ | |
combined.addToTail(listOneCurrentNode); | |
listOneCurrentNode = listOneCurrentNode.next | |
} | |
return combined.tail; | |
} | |
// ------------------------- | |
// Given: Singly Linked List | |
// ------------------------- | |
class Node { | |
constructor(val, next) { | |
this.val = val; | |
this.next = next; // error here, i had null | |
} | |
getNextNode(){ | |
return this.next; | |
} | |
} | |
class linkedList { | |
constructor(){ | |
this.head = null; // error here, i had head | |
this.next = null; | |
this.length = 0; | |
} | |
addToTail(val){ | |
const newNode = new Node(val); | |
if (!this.head) { | |
this.head = newNode; | |
} else { | |
this.tail.next = newNode; | |
} | |
this.tail = newNode; | |
this.length++; | |
return this; | |
} | |
} | |
const listOne = new linkedList() | |
const listTwo = new linkedList() | |
listOne.addToTail(1); | |
listOne.addToTail(2); | |
listOne.addToTail(6); | |
listTwo.addToTail(3); | |
listTwo.addToTail(5); | |
listTwo.addToTail(7); | |
listTwo.addToTail(9); | |
const result = combinedList(listOne, listTwo) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment