Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Data Structures/LinkedList/Palindrome_Linked_List
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Function to check if a singly linked list is palindrome

// APPROACH - 1 (USING STACK)

-> COMPLEXITY := O(N)

ALGORITHM:
- A simple solution is to use a stack of list nodes. This mainly involves three steps.
- Traverse the given list from head to tail and push every visited node to stack.
- Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node.
- If all nodes matched, then return true, else false.

// IMPLEMENTATION

class Solution{
public:
//Function to check whether the list is palindrome.
bool isPalindrome(Node *head)
{
//Your code here
Node *ptr = head;
//declare a stack
stack<int> s;

int i=0, top=-1, f=0;
while(ptr != NULL) {
//top = top + 1;
//arr[top] = ptr->data;
s.push(ptr->data);
ptr = ptr->next;
}

while(head != NULL) {
int p = s.top();
//top = top -1;
s.pop();

if(head->data != p) {
return false;
}

head = head->next;

}
return true;
}
};