Learner Level

Implement the palindromeLinkedListCheck method that checks whether the linked list values form a palindrome.

The linked list is provided as an integer array nodes, where each value represents one node from head to tail.

Your task is to check whether the linked list is a palindrome. A linked list is a palindrome when the values read the same from the beginning and from the end.

For example, [1,2,2,1] is a palindrome, but [1,2] is not because the first and last values are different.

Example 1

Input
nodes (int[]) = [1,2,2,1]
size (int) = 4
Returns (boolean) true

Example 2

Input
nodes (int[]) = [1,2]
size (int) = 2
Returns (boolean) false

Example 3

Input
nodes (int[]) = [1]
size (int) = 1
Returns (boolean) true

Compare the list values from both ends.

Use one pointer at the first element and another pointer at the last element. If both values are equal, move the left pointer forward and the right pointer backward. If any pair does not match, the linked list cannot be a palindrome.

If all opposite pairs match until the pointers meet or cross, return true.

Pseudocode:

function palindromeLinkedListCheck(nodes, size):
    left = 0
    right = size - 1
    while left < right:
        if nodes[left] != nodes[right]:
            return false
        left++
        right--
    return true
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.