File tree 1 file changed +14
-8
lines changed
1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change 1
1
var mergeKLists = function ( lists ) {
2
- let a = [ ]
2
+ let a = [ ] // create an array
3
+ // loop over all the lists
3
4
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
5
7
while ( k != null ) {
8
+ // push listnode value in a
6
9
a . push ( k . val )
10
+ // go to next node
7
11
k = k . next
8
12
}
9
13
}
14
+ // sort the array
10
15
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
13
19
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 ] )
17
22
}
23
+ // return ans List
18
24
return ans . next
19
- } ;
25
+ } ;
You can’t perform that action at this time.
0 commit comments