File tree Expand file tree Collapse file tree 1 file changed +7
-15
lines changed Expand file tree Collapse file tree 1 file changed +7
-15
lines changed Original file line number Diff line number Diff line change @@ -4,34 +4,26 @@ const Node = require('./node');
44LinkedList . prototype . partition = partition ;
55
66function partition ( value ) {
7- let left = null , right = null ;
7+ let head = this . head , tail = this . head ;
88
99 // partition list
1010 let current = this . head ;
1111 while ( current ) {
1212 const next = current . next ;
13- current . next = null ;
1413
1514 if ( current . data < value ) {
16- left = append ( left , current ) ;
15+ current . next = head ;
16+ head = current ;
1717 } else {
18- right = append ( right , current ) ;
18+ tail . next = current ;
19+ tail = current ;
1920 }
2021
2122 current = next ;
2223 }
24+ tail . next = null ;
2325
24- // merge list
25- let lastLeft ;
26- for ( lastLeft = left ; lastLeft . next ; lastLeft = lastLeft . next ) { }
27- lastLeft . next = right ;
28- this . head = left ;
29- }
30-
31-
32- function append ( head , node ) {
33- if ( head ) { node . next = head ; }
34- return node ;
26+ this . head = head ;
3527}
3628
3729// testing
You can’t perform that action at this time.
0 commit comments