File tree Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Expand file tree Collapse file tree 1 file changed +9
-10
lines changed Original file line number Diff line number Diff line change @@ -10,25 +10,24 @@ LinkedList.prototype.isPalindrome = function() {
1010 let slow = this . head ;
1111 let fast = this . head ;
1212
13- if ( fast . next === null ) {
14- return true ;
15- }
16-
17- stack . push ( slow . data ) ;
18-
1913 // collect element up to the middle
20- while ( fast . next && fast . next . next ) {
14+ while ( fast && fast . next ) {
15+ stack . push ( slow . data ) ;
2116 slow = slow . next ;
2217 fast = fast . next . next ;
23- if ( fast . next ) { stack . push ( slow . data ) ; }
2418 }
2519
26- // compare they are the same as the second half
27- while ( slow . next ) {
20+ // if odd number of elements
21+ if ( fast ) {
2822 slow = slow . next ;
23+ }
24+
25+ // compare they are the same as the second half
26+ while ( slow ) {
2927 if ( stack . pop ( ) !== slow . data ) {
3028 return false ;
3129 }
30+ slow = slow . next ;
3231 }
3332
3433 return true ;
You can’t perform that action at this time.
0 commit comments