# Check if a doubly linked list of characters is palindrome or not

Given a doubly linked list of characters, write a function that returns true if the given doubly linked list is palindrome, else false. ## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

1. Create a doubly linked list where each node contains only one character of a string.
2. Initialize two pointers left at starting of list and right at the end of the list.
3. Check the data at left node is equal to right node, if it is equal then increment left and decrement right till middle of the list, if at any stage it is not equal then return false.

## C++

 `// C++ program to check if doubly ` `// linked list is palindrome or not ` `#include ` `using` `namespace` `std; ` ` `  `// Structure of node ` `struct` `Node ` `{ ` `    ``char` `data; ` `    ``struct` `Node *next; ` `    ``struct` `Node *prev; ` `}; ` ` `  `/* Given a reference (pointer to pointer) to ` `   ``the head of a list and an int, inserts a ` `   ``new node on the front of the list. */` `void` `push(``struct` `Node** head_ref, ``char` `new_data) ` `{ ` `    ``struct` `Node* new_node = ``new` `Node; ` `    ``new_node->data  = new_data; ` `    ``new_node->next = (*head_ref); ` `    ``new_node->prev = NULL; ` `    ``if` `((*head_ref) !=  NULL) ` `      ``(*head_ref)->prev = new_node ; ` `    ``(*head_ref)    = new_node; ` `} ` ` `  `// Function to check if list is palindrome or not ` `bool` `isPalindrome(``struct` `Node *left) ` `{ ` `    ``if` `(left == NULL) ` `       ``return` `true``; ` ` `  `    ``// Find rightmost node ` `    ``struct` `Node *right = left; ` `    ``while` `(right->next != NULL) ` `        ``right = right->next; ` ` `  `    ``while` `(left != right) ` `    ``{ ` `        ``if` `(left->data != right->data) ` `            ``return` `false``; ` ` `  `        ``left = left->next; ` `        ``right = right->prev; ` `    ``} ` ` `  `    ``return` `true``; ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``struct` `Node* head = NULL; ` `    ``push(&head, ``'l'``); ` `    ``push(&head, ``'e'``); ` `    ``push(&head, ``'v'``); ` `    ``push(&head, ``'e'``); ` `    ``push(&head, ``'l'``); ` ` `  `    ``if` `(isPalindrome(head)) ` `        ``printf``(``"It is Palindrome"``); ` `    ``else` `        ``printf``(``"Not Palindrome"``); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Python3 program to check if doubly  ` `# linked list is a palindrome or not  ` ` `  `class` `Node:  ` `  `  `    ``def` `__init__(``self``, data, ``next``, prev): ` `        ``self``.data ``=` `data ` `        ``self``.``next` `=` `next` `        ``self``.prev ``=` `prev ` ` `  `# Given a reference (pointer to pointer) to  ` `# the head of a list and an int, inserts  ` `# a new node on the front of the list.  ` `def` `push(head_ref, new_data):  ` `  `  `    ``new_node ``=` `Node(new_data, head_ref, ``None``)  ` ` `  `    ``if` `head_ref !``=` `None``: ` `        ``head_ref.prev ``=` `new_node ` `        ``head_ref ``=` `new_node ` `         `  `    ``return` `head_ref ` `  `  `# Function to check if list is palindrome or not  ` `def` `isPalindrome(left):  ` `  `  `    ``if` `left ``=``=` `None``:  ` `        ``return` `True`  ` `  `    ``# Find rightmost node  ` `    ``right ``=` `left  ` `    ``while` `right.``next` `!``=` `None``:  ` `        ``right ``=` `right.``next`  ` `  `    ``while` `left !``=` `right: ` `      `  `        ``if` `left.data !``=` `right.data:  ` `            ``return` `False`  ` `  `        ``left ``=` `left.``next`  `        ``right ``=` `right.prev  ` `      `  `    ``return` `True`  `  `  `# Driver program  ` `if` `__name__ ``=``=` `"__main__"``:  ` `  `  `    ``head ``=` `None`  `    ``head ``=` `push(head, ``'l'``)  ` `    ``head ``=` `push(head, ``'e'``)  ` `    ``head ``=` `push(head, ``'v'``)  ` `    ``head ``=` `push(head, ``'e'``)  ` `    ``head ``=` `push(head, ``'l'``)  ` ` `  `    ``if` `isPalindrome(head):  ` `        ``print``(``"It is Palindrome"``)  ` `    ``else``: ` `        ``print``(``"Not Palindrome"``)  ` ` `  `# This code is contributed by Rituraj Jain `

Output:

```It is Palindrome
```

Time complexity: O(n)
Auxiliary Space : O(1)

Related Post:

This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Improved By : rituraj_jain

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.