DevPath

← All problems

Palindrome Linked List

Easy linked-list · two-pointers

Description

Return true if the sequence of values reads the same forwards and backwards.

Constraints

0 <= n <= 100000

Examples

Input: list = [1,2,2,1]

Output: true

Hints (1)
  • Find the middle, reverse the second half and compare.

Solutions

Two pointers inward. · Time O(n) · Space O(1)

func isPalindromeList(list []int) bool {
    for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
        if list[i] != list[j] {
            return false
        }
    }
    return true
}

Create a free account to submit solutions, track progress and save problems.

Create account