← Home

234. Palindrome Linked List

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
234

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 234. Palindrome Linked List, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are isPalindrome, reverse.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 36 ms, faster than 18.01% of C++ online submissions for Palindrome Linked List.
02//Memory Usage: 13.3 MB, less than 27.08% of C++ online submissions for Palindrome Linked List.
03
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 *     int val;
08 *     ListNode *next;
09 *     ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14    bool isPalindrome(ListNode* head) {
15        vector<int> v;
16        ListNode* cur = head;
17        while(cur){
18            v.push_back(cur->val);
19            cur = cur->next;
20        }
21        for(int i = 0; i < v.size()/2; i++){
22            if(v[i] != v[v.size()-1-i]) return false;
23        }
24        return true;
25    }
26};
27
28//https://leetcode.com/problems/palindrome-linked-list/discuss/64501/Java-easy-to-understand
29//slow and fast pointer
30//Runtime: 24 ms, faster than 98.19% of C++ online submissions for Palindrome Linked List.
31//Memory Usage: 12.6 MB, less than 68.90% of C++ online submissions for Palindrome Linked List.
32
33/**
34 * Definition for singly-linked list.
35 * struct ListNode {
36 *     int val;
37 *     ListNode *next;
38 *     ListNode(int x) : val(x), next(NULL) {}
39 * };
40 */
41class Solution {
42public:
43    bool isPalindrome(ListNode* head) {
44        ListNode *slow = head, *fast = head;
45        
46        while(fast!=NULL && fast->next!=NULL){
47            slow = slow->next;
48            fast = fast->next->next;
49        }
50        
51        if(fast != NULL){
52            //the length of list is odd
53            //in this situation we should ignore the middle node
54            slow = slow->next;
55        }
56        
57        //slow: later part of original list
58        //fast: former part of original list
59        slow = reverse(slow);
60        fast = head;
61        
62        while(slow != NULL){
63            if(slow->val != fast->val) return false;
64            slow = slow->next;
65            fast = fast->next;
66        }
67        
68        return true;
69    }
70    
71    ListNode* reverse(ListNode* head){
72        //reverse the later part of original list
73        ListNode* prev = NULL;
74        ListNode* tmp;
75        while(head != NULL){
76            tmp = head->next;
77            head->next = prev;
78            prev = head;
79            head = tmp;
80        }
81        //when the loop finishes, head is NULL, 
82        //and prev is the last node in the original list
83        return prev;
84    }
85};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.