check if linked list is palindrome or not 3f

Solutions on MaxInterview for check if linked list is palindrome or not 3f by the best coders in the world

showing results for - "check if linked list is palindrome or not 3f"
Riccardo
17 Nov 2016
1#include<bits/stdc++.h>
2 
3using namespace std;
4 
5// Declaration of a single Node
6class Node {
7public:
8        int data;
9        Node(int d){
10            data = d;
11        }
12        Node *ptr;
13};
14 
15// Function that returns boolean value
16bool isPalin(Node* head){
17        
18        // Temp pointer
19        Node* slow= head;
20 
21        // Create a stack
22        stack <int> s;
23 
24 
25        // First traversal to push all the elements to stack
26        while(slow != NULL){
27                s.push(slow->data);
28                slow = slow->ptr;
29        }
30 
31        // Second Traversal to compare the stack and node
32        while(head != NULL ){
33            
34            int i=s.top();
35            s.pop();
36 
37            // Compare data
38            if(head -> data != i){
39                return false;
40            }
41        head=head->ptr;
42        }
43 
44return true;
45}
46 
47// Driver Function
48int main(){
49    // Create nodes
50    Node one = Node(31);
51    Node two = Node(32);
52    Node three = Node(33);
53    Node four = Node(34);
54    Node five = Node(35);
55 
56    // Connect all the nodes
57    five.ptr = NULL;
58    one.ptr = &two;
59    two.ptr = &three;
60    three.ptr = &four;
61    four.ptr = &five;
62    Node* temp = &one;
63 
64    
65    // Call function to return bool if the list is palindrome or not
66    int result = isPalin(&one);
67 
68    if(result == 1)
69            cout<<"The value is True\n";
70    else
71        cout<<"The value is False\n";
72 
73return 0;
74}
queries leading to this page
how to check if a linked list is palindromecheck if linked list is palindrome using recursionto check if given linked list is palindromecheck if linked list is palindrome leetcodepalindrom linked listpalindrome in linked listhow to check if linked list is palindromecheck if a linked list is palindrome or not pythonlinked list is a palindromegiven a singly linked list 2c determine if it is a palindrome how to check if a linked list is a palindromelinked list is palindrome or not o 281 29 spacepalindrome linked list using stackwrite an algorithm to check whether the given string is palindrome or not using linked listwrite a function to validate whether or not a linked list is palindromhow to check a linked list is palindrome234 palindrome linked listlinked list of chars is a palindromepalindrome linked list in c 2b 2bcheck if linked list is palindrome gfgcheck if linked list is palindrome or not javafind if linked list is palindromehow to check palindrome in linked listcheck if the linked list is palindrome or notcheck linked list is palindromecheck if a linked list is palindromefind if a linked list is a palindromecheck if the linked list is a palindromepython check if linked list is palindromelinked list of a string form a palindrome practicecheck if given linked list is palindromegiven a linked list 2c tell if it is a palindrome or not15 check if linked list is palindrome gfg practicepalindrome linked list possible with recusrionpalindrome test linked listfind linked list is palindrome or notpalindrome linked list gfg practicec 2b 2b program to identify the linked list is palindrome or notlinkedlist with palindromelinked list is palindromeis linked list palindrom inplacecheck whether linked list is palindrome or notpalindrome linked list recusrioncheck if a linkedlist is palindrome or not linked list palindromecheck if a singly linked list is palindromepalindrome of linked listhow to check whether a linked list is palindrome or notfind linked list is palindrome or not 2clinked list strings check palindrome without making stringintuit palindrome linkedlist questioncheck if a linked list is palindrome or notcheck wthether a given linked list is palindrome or notpalindrome using doubly linked list in cpalindrome linkedlistcheck if linked list is palindrome pythoncheck for palindrome linked listcheck palindrome linked list in o 281 29 space check palindrome in linked listto check singly linked list is a palindrome or not 28o 28n 29 and o 281 29 29is palindrome linkedlistfunction to check if a singly linked list is palindromedetect if a string represented through linked list is a palindrome or notcheck if linked list is palindromehow to check palindrome in linked list in o 281 29check if linked list is palindrome gfg practicecheck if a linked list is palindrome or not leetcode create palindrome linked list solutioncheck if a linkedlist is palindrome or notis linked list palindromelinked list of chars is a palindrome gfgcheck if linked list is palindrome in ccheck if linkedlist is palindrome or notlinked list is palindrome or notin palindrome land 2c every linked list is a palindrome and you are the palindrome policepalindrome linked listcheck linkedlist palindromecheck palindrome in linked list recusrioncheck palindrome linked listcheck if linked list is palindrome or not 3f