1
2
3
2
1
[0][1][2][3][4]
Brute force · copy to array
▸1given head2copy all values into arr3L = 0, R = n − 14while L < R: if arr[L++] ≠ arr[R−−]: return false5return true
state
- n5
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 problemGiven the head of a singly linked list, return whether the sequence of node values reads the same forward and backward.
▸1given head2copy all values into arr3L = 0, R = n − 14while L < R: if arr[L++] ≠ arr[R−−]: return false5return true
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.