Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5602618

Browse filesBrowse files
author
unknown
committed
add doc to 21
1 parent 638bbcf commit 5602618
Copy full SHA for 5602618

File tree

Expand file treeCollapse file tree

1 file changed

+11
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+11
-0
lines changed

‎21-30/21. Merge Two Sorted Lists.js

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
var mergeTwoLists = function(list1, list2) {
2+
// add the list node to a variable
23
let l = new ListNode(0);
34
let head = l
5+
// create a while loop and check if the argument (list1 and list2) is not empty
46
while(list1 != null && list2 != null){
7+
// if the value of list1 is less than list2
58
if(list1.val < list2.val){
9+
//add the value to the node list of 1
610
l.next = list1
711
list1 = list1.next
812
}else{
13+
// if not add the value to the node list of 2
914
l.next = list2
1015
list2 = list2.next
1116
}
17+
18+
//move on to the next node
1219
l = l.next
1320
}
21+
22+
//add the remaining node values to the merged list
1423
if (list1 == null) {
1524
l.next = list2;
1625
}
1726
else {
1827
l.next = list1;
1928
}
29+
30+
//return the combined sorted list
2031
return head.next
2132
};

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.