Created
May 22, 2021 13:35
-
-
Save timotew/03b7cae4535c8d9d0807ac3c8c0e8489 to your computer and use it in GitHub Desktop.
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
| var mergeTwoLists = function(l1, l2) { | |
| // declare our return list | |
| // go through the lists | |
| // compare the weight of nodes | |
| // add the node with the lightest weight | |
| // to our result list | |
| // continue till we get to the end of the lists | |
| let resultListTail = null; // [2,(3),5,6,7,7,] | |
| let resultListHead = null; | |
| let l1Current = l1; | |
| let l2Current = l2; | |
| // choosing the first item in the result | |
| // by checking both node for which one | |
| // has the lightest weigth in their first index | |
| if (l1Current === null || l2Current === null){ | |
| if (l1Current !== null) { | |
| resultListHead = l1Current; | |
| l1Current = l1Current.next; | |
| } else if (l2Current !== null) { | |
| resultListHead = l2Current; | |
| l2Current = l2Current.next; | |
| } | |
| } else if (l1Current.val <= l2Current.val) { | |
| resultListHead = l1Current; | |
| l1Current = l1Current.next; | |
| } else { | |
| resultListHead = l2Current; | |
| l2Current = l2Current.next; | |
| } | |
| if (resultListHead !== null) { | |
| resultListTail = resultListHead; | |
| resultListTail.next = null; | |
| } | |
| while(l1Current !== null && l2Current !== null) { | |
| // compareing the value of the current nodes | |
| // from the two lists we are given | |
| // we use node.val as the weight that determines what | |
| // node is added to our result list | |
| // if both node that we are looking at from both list | |
| // are of thesame weight (node1.val == node2.val) we add both node to our result | |
| // else we take the node with the smallest data | |
| if(l1Current.val < l2Current.val){ | |
| // l1 has smaller weight | |
| // add l1 to result | |
| // move both result and l1 pointers | |
| resultListTail.next = l1Current; | |
| l1Current = l1Current.next; | |
| resultListTail = resultListTail.next; | |
| resultListTail.next = null; | |
| } else if (l2Current.val < l1Current.val) { | |
| // l2 is smaller therefore | |
| // we add l2 to our result | |
| // moive result pointer also the l2 pointer | |
| // [1] | |
| resultListTail.next = l2Current; | |
| // [(1),1,3,4] | |
| resultListTail = resultListTail.next; | |
| l2Current = l2Current.next; | |
| // [1,(1),3,4] | |
| resultListTail.next = null; | |
| // [1,(1)->null] | |
| } else { | |
| //both nodes have thesame weight | |
| // add node of l1 to result | |
| // move result pointer | |
| // move l1 poniter | |
| resultListTail.next = l1Current; | |
| resultListTail = resultListTail.next; | |
| l1Current = l1Current.next; | |
| resultListTail.next = null; | |
| // add node of l2 to result | |
| // move result pointer | |
| // move l2 pointer | |
| resultListTail.next = l2Current; | |
| resultListTail = resultListTail.next; | |
| l2Current = l2Current.next; | |
| resultListTail.next = null; | |
| } | |
| } | |
| if (l1Current !== null) { | |
| // [1 -> (2) -> null] = ResultList | |
| //[(3) -> 4] == l1 | |
| resultListTail.next = l1Current; | |
| // [1 -> (2) -> 3 -> 4 -> null] | |
| } | |
| if (l2Current !== null) { | |
| resultListTail.next = l2Current; | |
| } | |
| return resultListHead; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment