-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_234.cpp
More file actions
134 lines (111 loc) · 2.65 KB
/
leetcode_234.cpp
File metadata and controls
134 lines (111 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 234. Palindrome Linked List
// https://leetcode.com/problems/palindrome-linked-list/
/*
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?
*/
#include <iostream>
#include <vector>
using namespace std;
// 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) {}
};
// brute force approach
// 1. Traverse the linked list and store the values in a vector.
// 2. Use two pointers to compare the values from the start and end of the vector.
// 3. If all values match, return true; otherwise, return false.
// time complexity: O(n)
// space complexity: O(n)
class Solution
{
public:
bool isPalindrome(ListNode *head)
{
if (!head)
return true;
if (!head->next)
return true;
ListNode *curr = head;
vector<int> r;
while (curr != nullptr)
{
r.push_back(curr->val);
curr = curr->next;
}
int i = 0, j = r.size() - 1;
while (i < j)
{
if (r[i] != r[j])
return false;
i++;
j--;
}
return true;
}
};
// using pointers approach
// time complexity O(n)
// space complexity O(1)
bool isPalindrome(ListNode *head)
{
if (!head)
return false;
ListNode *fast = head;
ListNode *slow = head;
while (fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
}
if (fast)
slow = slow->next;
ListNode *prev = nullptr;
ListNode *curr = slow;
while (curr)
{
ListNode *temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
fast = head;
while (prev)
{
if (prev->val != fast->val)
return false;
prev = prev->next;
fast = fast->next;
}
return true;
}
int main()
{
// Example usage
ListNode *head = new ListNode(1, new ListNode(2, new ListNode(2, new ListNode(1))));
Solution solution;
bool result = solution.isPalindrome(head);
cout << (result ? "true" : "false") << endl; // Output: true
// Clean up memory
while (head != nullptr)
{
ListNode *temp = head;
head = head->next;
delete temp;
}
return 0;
}