-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckIfLinkedListIsPalindrome.cpp
More file actions
60 lines (54 loc) · 1.63 KB
/
Copy pathCheckIfLinkedListIsPalindrome.cpp
File metadata and controls
60 lines (54 loc) · 1.63 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
/*
Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
Example 1:
Input:
N = 3
value[] = {1,2,1}
Output: 1
Explanation: The given linked list is
1 2 1 , which is a palindrome and
Hence, the output is 1.
Example 2:
Input:
N = 4
value[] = {1,2,3,4}
Output: 0
Explanation: The given linked list
is 1 2 3 4 , which is not a palindrome
and Hence, the output is 0.
Your Task:
The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.
Expected Time Complexity: O(N)
Expected Auxialliary Space Usage: O(1) (ie, you should not use the recursive stack space as well)
*/
/// No Linear Extra Space
class Solution{
public:
//Function to reverse the LL
Node* makeReverse(Node *head){
if(!head or !head->next) return head;
Node *reverse=makeReverse(head->next);
head->next->next=head;
head->next=NULL;
return reverse;
}
//Function to check whether the list is palindrome.
bool isPalindrome(Node *head)
{
if(!head or !head->next) return true;
Node *slow=head;
Node *fast=head->next;
while(fast and fast->next){
slow=slow->next;
fast=fast->next->next;
}
Node *reverse=makeReverse(slow->next);
slow=head;
while(slow and reverse){
if(slow->data!=reverse->data) return false;
slow=slow->next;
reverse=reverse->next;
}
return true;
}
};