-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234. Palindrome Linked List.cpp
144 lines (114 loc) · 3.34 KB
/
234. Palindrome Linked List.cpp
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
135
136
137
138
139
140
141
142
143
144
/*
Link: https://leetcode.com/problems/palindrome-linked-list/
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// Using Stack
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(!head)
return true;
stack<int> st;
ListNode *p = head;
while(p) {
st.push(p->val);
p = p -> next;
}
p = head;
while(p) {
if (p -> val == st.top()) {
st.pop();
p = p -> next;
} else {
return false;
}
}
return true;
}
};
// Recursive Solution
// The approaches uses the concept that once we reach that last node of the list, we hace acces to first and last node.
// Then, we compare them and if they are equal, we compare the next of first and second last from the last.
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(!head || !head -> next) // if there either no node or signle node
return true;
return checkPalin(&head, head->next);
}
bool checkPalin (ListNode **first, ListNode *last) {
if(!last) { // when we have reached the last node of the list
return true;
}
bool isPalin = checkPalin (first, last -> next);// calling for the next of last
if (!isPalin) {
return false;
}
isPalin = (*first) -> val == last -> val; // checking for the value of
*first = (*first) -> next;
return isPalin;
}
};
/*
TC = O(n)
SC = O(n)
*/
// In place by reversing the second half of the list
// The approach uses the concept of reversing the second half of the list and then comparing its first element
// to first element of original list and so on.
class Solution {
public:
bool isPalindrome(ListNode* head) {
if (!head || !head -> next)
return true;
if (!head -> next -> next) {
return head -> val == head -> next -> val;
}
ListNode *ptr = head, *fast = head, *slow = NULL;
while(fast && fast -> next) {
fast = fast -> next -> next;
if (!slow)
slow = head;
else
slow = slow -> next;
}
if (fast) {
slow = slow -> next;
}
slow -> next = reverse(slow -> next);
slow = slow -> next;
while (slow) {
if (ptr -> val != slow -> val)
return false;
slow = slow -> next;
ptr = ptr -> next;
}
return true;
}
ListNode* reverse (ListNode* head) {
ListNode *prev = NULL, *curr = head, *next = NULL;
while(curr) {
next = curr -> next;
curr -> next = prev;
prev = curr;
curr = next;
}
return prev;
}
};