0

I am working on a problem to determine whether a linked list is a Palindrome (Leetcode 234). I came across this (way above my level) solution.

var isPalindrome = function(head) {
    let slow = head, fast = head, prev, temp

    while (fast && fast.next)
        slow = slow.next, fast = fast.next.next
    prev = slow, slow = slow.next, prev.next = null

    while (slow)
        temp = slow.next, slow.next = prev, prev = slow, slow = temp
    fast = head, slow = prev

    while (slow) {
        if (fast.val !== slow.val) return false
        else fast = fast.next, slow = slow.next
    } //i added these curly braces because the solution still works with them
    return true
};

I was confused by the lack of curly braces so I added them to see if the loops were nested.

However, when I add curly braces to the second while loop, the code times out and if I add them to the first while loop, some test cases fail.

var isPalindrome = function(head) {
    let slow = head, fast = head, prev, temp
    while (fast && fast.next) {
        slow = slow.next, fast = fast.next.next
    prev = slow, slow = slow.next, prev.next = null
    }
    while (slow) {
        temp = slow.next, slow.next = prev, prev = slow, slow = temp
    fast = head, slow = prev;
    }
    while (slow) {
        if (fast.val !== slow.val) return false
        else fast = fast.next, slow = slow.next
      }
    return true
};

I also tried nesting the third while loop within the second like so,

var isPalindrome = function(head) {
    let slow = head, fast = head, prev, temp
    while (fast && fast.next) {
        slow = slow.next, fast = fast.next.next
    prev = slow, slow = slow.next, prev.next = null
    }
    while (slow) {
        temp = slow.next, slow.next = prev, prev = slow, slow = temp
    fast = head, slow = prev;
    while (slow) {
        if (fast.val !== slow.val) return false
        else fast = fast.next, slow = slow.next
      }
    }
    return true
};

and it still does not run. I have tried every variation of nesting possible with no luck, and I don't even know what to Google so I can understand.

what am I missing here? I am so confused

any help would be extremely appreciated. Thank y'all

blakkwater
  • 1,133
  • 7
  • 13
  • 1
    The syntax for a while loop is `while` `(` _Expression_ `)` _Statement_ — see [MDN](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/while) and [the specification](//tc39.es/ecma262/#sec-while-statement). The _Statement_ is `temp = slow.next, slow.next = prev, prev = slow, slow = temp`. Why do you think `fast = head, slow = prev` would also be part of the statement? See [Are braces necessary in one-line statements in JavaScript?](/q/4797286/4642212). – Sebastian Simon May 27 '23 at 05:10
  • @SebastianSimon never seen code that looks like this before, thought the statement would end with a semicolon, not just a new line. But thank u for the clarification :) – eatthischair May 27 '23 at 16:13

1 Answers1

0

The code you found is not "above your level" at all. On the contrary it is so bad that am not surprised if anyone found it confusing, let alone a beginner. The trick the original code uses, sadly, is to convert multiple statements into single inline statements using the comma operator so as to avoid typing curly braces, and leaving out semicolon terminators to save space.

Here's how a maintenance programmer might have written it by mechanistically

  • replacing comma operator statements with block statements,
  • inserting statement terminators,
  • moving temp to the only block that uses it, and
  • not putting an "else" keyword after a conditional return statement:
var isPalindrome = function(head) {
    let slow = head, fast = head, prev;

    while (fast && fast.next) {
        slow = slow.next;
        fast = fast.next.next;
    }
    prev = slow;
    slow = slow.next;
    prev.next = null;
    while (slow) {
        let temp = slow.next;
        slow.next = prev;
        prev = slow;
        slow = temp;
    }
    fast = head;
    slow = prev;
    while (slow) {
        if (fast.val !== slow.val) {
            return false;
        }
        fast = fast.next;
        slow = slow.next;
    }
    return true;
};

I am not commenting on whether the code works or not, but written this way, inserting curly braces to find out if any of the while loops is nested is unnecessary.


Title Questions

  1. Why does JavaScript view while loops with and without curly braces differently? It doesn't make such a distinction. The compiler is looking for a statement after while, and curly braces and code within them are parsed as a single block statement.

  2. Can while loops without curly braces be nested? Yes, but not usually (as in "never") as an inline statement immediately after the outer while:

     while(condition1) while(condition2){ code }
    
traktor
  • 17,588
  • 4
  • 32
  • 53
  • Thank you for this, this is how I was taught to write code. Everyone in the comments was praising them and I couldn't figure out what was going on! lol – eatthischair May 27 '23 at 18:51