Skip to content

234. Palindrome Linked List

Difficulty Topics

Description

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

 

Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

Input: head = [1,2]
Output: false

 

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 9

 

Follow up: Could you do it in O(n) time and O(1) space?

Solution

palindrome-linked-list.py
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:

        lst = []

        while head:
            lst.append(head.val)
            head = head.next

        return lst == lst[::-1]
palindrome-linked-list.cpp
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        if (head == nullptr || head->next == nullptr) return true;

        ListNode *slow = head, *fast = head;

        while (fast->next && fast->next->next){
            slow = slow->next;
            fast = fast->next->next;
        }

        slow->next = reverseLL(slow->next);
        slow = slow->next;

        while (slow){
            if (head->val != slow->val) return false;

            head = head->next;
            slow = slow->next;
        }

        return true;

    }

    ListNode* reverseLL(ListNode *node){
        ListNode *res = NULL;

        while (node){
            ListNode *next = node->next;
            node->next = res;
            res = node;
            node = next;
        }

        return res;
    }
};