Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Linked List Palindrome Leet Code Solution in Javascript

I have looked up the solution for the palindrome question in Javascript.

There is a line of code that I dont really understand and I hope someone can enlighten me.

Here is the solution:

this.palindrom = function() {
            //two pointers to find the middle 
            // 1 slow pointer - move 1 at a time
            // 1 fast pointer - move 2 at a time 

        let slow = this.head
        let fast = this.head
        let start = this.head
        console.log('fast', fast)
        let length = 0 
       
        while( fast && fast.next) {
            fast = fast.next.next
            slow = slow.next 
            start = start.next
            length++
        }
        console.log(slow)
       let mid = this.reverse(slow)
        console.log('mid',mid)
        while (length !== 0) {
            length --
            if (mid.data !== start.data) return false 
            else return true 
        }

      }
    }

I do not understand why the parameters of the "while" loop is

while( fast && fast.next)

i typed while(fast.next) and there was an error saying cannot access null of next. I am wondering why fast && fast.next works instead.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

fast && fast.next is applying a safeguard to fast being null. If fast is null, then it would be an error to evaluate fast.next -- which is what you experienced when you "simplified" the condition.

If fast is null then the expression fast will be "falsy", and because of how the && operator works (short-circuiting), the evaluation will then stop and not attempt to evaluate fast.next. Instead it will consider the whole expression to be falsy, and exit the loop.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!