8000 update code & comment 23. Merge k Sorted Lists.js · Tahirc1/LeetcodeJs@e066e5e · GitHub
[go: up one dir, main page]

Skip to content

Commit e066e5e

Browse files
authored
update code & comment 23. Merge k Sorted Lists.js
1 parent ef5e6ee commit e066e5e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

21-30/23. Merge k Sorted Lists.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
var mergeKLists = function(lists) {
2-
let a = []
2+
let a = [] // create an array
3+
// loop over all the lists
34
for(let i = 0 ; i < lists.length ; i++){
4-
let k = lists[i]
5+
let k = lists[i] // store list in k
6+
// loop till end of list in k
57
while(k != null){
8+
// push listnode value in a
69
a.push(k.val)
10+
// go to next node
711
k = k.next
812
}
913
}
14+
// sort the array
1015
a.sort(function(a,b){return a-b})
11-
let m = new ListNode(0)
12-
let ans = m
16+
let m = new ListNode(0) // create listnode
17+
let ans = m // store head of listnode in ans
18+
// loop the array
1319
for(let j = 0 ; j< a.length ;j++){
14-
let n = new ListNode(a[j])
15-
m.next = n
16-
m = m.next
20+
// assign array j value to next listnode and go to next node
21+
m = m.next = new ListNode(a[j])
1722
}
23+
// return ans List
1824
return ans.next
19-
};
25+
};

0 commit comments

Comments
 (0)
0