Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.

Try one problem
0
Problem

Palindrome Linked List

LeetCode #234Easy
Middle + reverse half + two pointers

Given the head of a singly linked list, return whether the sequence of node values reads the same forward and backward.

Asked atAmazonMetaMicrosoft
step 1 / 14
1
2
3
2
1
[0][1][2][3][4]
Brute force · copy to array
1given head
2copy all values into arr
3L = 0, R = n − 1
4while L < R: if arr[L++] ≠ arr[R−−]: return false
5return true
state
  • n5

line 1Is the list the same forwards and backwards? Lists only walk FORWARD — you cannot run a pointer from the tail. Easy fix: copy the values into an array first.