oneby7

Linked Lists Fast and slow pointers

291. Palindrome Linked List

Easy

You are given head, the first node of a singly linked list. Return true if the list is a palindrome, and false otherwise.

A list is a palindrome when its values read the same from the head to the last node as from the last node back to the head. Each node holds a whole number, and two values match only when they are the same number.

The examples write a list as its values from the head on, so head = [4,1,6] is the list 4 → 1 → 6.

Example 1

37073

Input: head = [3, 7, 0, 7, 3]

Output: true

Explanation: Read from the last node back to the head, the values are 3, 7, 0, 7, 3, the same as from the head on. The middle value 0 has no partner, and it does not need one.

Example 2

40-9940

Input: head = [40, -9, 9, 40]

Output: false

Explanation: The first and last values are both 40, but the second value is -9 and the second from the end is 9. Those are different numbers, so the list is not a palindrome.

Example 3

1221

Input: head = [12, 21]

Output: false

Explanation: Backwards the list is 21 → 12, which is a different list. The digits 1, 2, 2, 1 read the same both ways, but the list holds two numbers, 12 and 21, not four digits.

Check your understanding

Work out the answer for this input before you write any code.

head = [3, 30, 3]

Constraints

  • 1 ≤ number of nodes ≤ 105
  • -109 ≤ Node.val ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.