Python Program to Check whether a Singly Linked List is a Palindrome










Python Program to Check whether a Singly Linked List is a Palindrome



Description:
This program uses the Linked List data structure. We will implement the stack data structure to check if the linked list is a palindrome or not. We traverse the linked list two times.
In the first traversal, every element of the linked list from head to tail is pushed onto the stack.
In the second traversal, we check if each element is equal to the element popped from the top of the stack or not.
If every element during the second traversal is equal to the popped element, then the linked list is a palindrome.

Python Code:
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.first = None def insert(self, data): newNode = Node(data) if self.first is None: self.first = newNode else: temp = self.first while temp.next is not None: temp = temp.next temp.next = newNode def display(self): print("\nLinked List: ", end='') temp = self.first while temp is not None: print(temp.data, end='->') temp = temp.next print(None) def check_palindrome(self): stack = [] temp = self.first while temp is not None: stack.append(temp.data) temp = temp.next temp = self.first while temp is not None: popped = stack.pop() if temp.data == popped: temp = temp.next else: return False return True def main(): elements = list(map(int, input("Enter elements of the linked list: ").split(' '))) obj = LinkedList() for data in elements: obj.insert(data) obj.display() if obj.check_palindrome(): print("Linked List is a Palindrome!") else: print("Linked List is not a Palindrome!") if __name__ == "__main__": main()

Output 1:
Enter elements of the linked list: 10 20 30 20 10
Linked List: 10->20->30->20->10->None Linked List is a Palindrome!

Output 2:
Enter elements of the linked list: 10 20 30 40
Linked List: 10->20->30->40->None Linked List is not a Palindrome!

More Articles of Siddhartha Dutta:

Name Views Likes
Python Program to Upload Files to Your Google Drive 805 0
Python Program to Upload Files to Your Dropbox 489 0
Python Program to Get a Function from a C Program File 275 0
Python DynamoDB Delete a Table 301 0
Python DynamoDB Scan the Table 407 0
Python DynamoDB Query the Table 296 0
Python DynamoDB Delete a Record 317 0
Python DynamoDB Update an Existing Record 301 0
Python DynamoDB Read a Record 297 0
Python DynamoDB Insert Multiple Records into Table 362 0
Python DynamoDB Insert Record into Table 315 0
Python DynamoDB Create Table 322 0
Python DynamoDB Get Started 392 0
Python Program to Test Hard Disk Speed 349 0
GUI based Python Program to Count Number of Characters 298 0
GUI based python application to count number of characters 350 3
Python MySQL prevent SQL injection in update 406 0
Python MySQL prevent SQL injection in delete 297 0
Python MySQL prevent SQL injection in select 695 0
Python MySQL join two tables 342 0
Python MySQL limit the number of records in a table 366 0
Python MySQL update existing record 303 0
Python MySQL drop table only if exist 300 0
Python MySQL drop table 297 0
Python MySQL delete record 302 0
Python MySQL sort the result in ascending order 289 0
Python MySQL sort the result in descending order 307 0
Python MySQL sort the result 273 0
Python MySQL Order By 288 0
Python MySQL wildcard select 276 0
Python MySQL select with a Filter 281 0
Python MySQL where example and usage 317 0
Python MySQL select using fetchone method 277 0
Python MySQL select particlular column from table 273 0
Python MySQL select from table 0 0
Python MySQL insert record into table and get inserted ID 293 0
Python MySQL insert multiple records into table 337 0
Python MySQL insert record into table 289 0
Python MySQL create table 286 0
Python MySQL check database exists or not 353 5
Python MySQL create database 297 11
Python MySQL connector get started 321 10
Python MongoDB Limit the Result 301 10
Python MongoDB Update Many Records 314 10
Python MongoDB Update Record 304 10
Python MongoDB Drop Collection 303 10
Python MongoDB Delete All Documents in a Collection 305 10
Python MongoDB Delete Many Documents 354 10
Python MongoDB Delete Document 324 10
Python MongoDB Sort the result in descending order 324 10
Python MongoDB Sort the Result 295 10
Python MongoDB Query Filter With Regular Expressions 342 10
Python MongoDB Query Filter the Result 320 10
Python MongoDB Select specific fields from collection 338 10
Python MongoDB Select all data from collection using find method 386 10
Python MongoDB Select data from collection using find_one method 314 10
Python MongoDB Insert Multiple Documents, with Specified IDs 314 10
Python MongoDB Insert Multiple Documents 327 10
Python MongoDB Insert Into Collection and return the _id field 320 10
Python MongoDB Insert Into Collection 319 10
Python MongoDB Check if Collection Exists 1532 10
Python MongoDB Creating a Collection 324 10
Python MongoDB Check if Database Exists 361 10
Python MongoDB Creating a Database 379 20
Python MongoDB Get Started 337 21
Python Program to Convert a given Singly Linked List to a Circular List 212 10
Python Program to Check whether a Singly Linked List is a Palindrome 874 10
Python Program to Illustrate the Operations of Singly Linked List 187 10
Python Program to Support Infinite Precision Arithmetic & Store a Number as a List of Digits 186 10
Python Program to Interchange the two Adjacent Nodes given a circular Linked List 187 10
Python Program to Interchange two Elements of the List without touching the Key Field 181 10
Python Program to Solve Josephus Problem using Linked List 623 10
Python Program to Implement Shell Sort 180 10
Python Program to Implement Comb Sort 190 10
Python Program to Implement Cocktail Shaker Sort 270 10
Python Program to Implement Gnome Sort 250 11
Python Program to Find the Sum of All Nodes in a Binary Tree 358 10
Python Program To Find the Smallest and Largest Elements in the Binary Search Tree 198 10
Python Program to Implement Priority Queue to Add and Delete Elements 171 10
Python Program to Implement Dequeue 220 10
Python Program to Flatten a List without using Recursion 174 10
Python Program to Flatten a Nested List using Recursion 297 10
Python Program to Convert a Binary Tree into a Singly Linked List by Traversing Level by Level 344 10
Python Program to Find the Total Sum of a Nested List Using Recursion 859 10
Python Program to Print the Alternate Nodes in a Linked List using Recursion 157 10
Python Program to Print the Alternate Nodes in a Linked List without using Recursion 170 10
Python Program to Implement Selection Sort 164 10
Python Program to Find Number of Occurrences of All Elements in a Linked List 231 16
Python Program to Display the Nodes of a Linked List in Reverse using Recursion 212 13
Python Program to Display the Nodes of a Linked List in Reverse without using Recursion 187 11
Python Program to Read a Linked List in Reverse 180 15
Python Program to Check String is Palindrome using Stack 227 14
Python Program to Read the Contents of a File in Reverse Order 264 20
Python Program to Count the Number of Blank Spaces in a Text File 245 16
Python Program to Read a Text File and Print all the Numbers Present in the Text File 242 12
Python Program that Reads a Text File and Counts the Number of Times a Certain Letter Appears in the Text File 276 27
Python Program to Count the Number of Vowels Present in a String using Sets 289 20
Python Program to Find the Second Largest Number in a List Using Bubble Sort 205 19
Python Program to Find the Length of a List Using Recursion 214 12
Python Program to Check if Expression is correctly Parenthesized 218 18
Python Program to Find the Fibonacci Series without Using Recursion 567 27
Python Program to find the factorial of a number without recursion 252 27
Python Program to Reverse a String without using Recursion 277 28
Python Program to Find the Binary Equivalent of a Number without Using Recursion 187 15
Python Program to Find All Numbers which are Odd and Palindromes Between a Range of Numbers without using Recursion 261 23
Python Program to Find the Sum of Digits in a Number without Recursion 238 23
Python Program to Sort the List According to the Second Element in Sublist 231 17
Python Program to Append, Delete and Display Elements of a List Using Classes 765 22
Python Program to Find the Area of a Rectangle Using Classes 230 18
Python Program to Create a Class and Get All Possible Subsets from a Set of Distinct Integers 314 31
Python Program to Create a Class in which One Method Accepts a String from the User and Another Prints it 219 15
Python Program to Create a Class which Performs Basic Calculator Operations 231 17
Python Program to Create a Class and Compute the Area and the Perimeter of the Circle 237 19
Python Program that Displays which Letters are in the First String but not in the Second 222 19
Python Program to Sort a List According to the Length of the Elements 247 18
Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List 602 24
Python Program to Find the Sum of the Digits of the Number Recursively 250 21
Python Program to Find the Binary Equivalent of a Number Recursively 243 20
Python Program to Find the Sum of Elements in a List Recursively 220 19
Python Program to Find the Factorial of a Number Using Recursion 171 19
Python Program to Find the Fibonacci Series Using Recursion 260 28
Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively 301 12
Python Program to Determine Whether a Given Number is Even or Odd Recursively 204 14
Python Program to Put Even and Odd elements in a List into Two Different Lists 350 20
Python Program to Find the Second Largest Number in a List 189 16
Python Program to Find the Largest Number in a List 277 22
Python Program to Clear the Rightmost Set Bit of a Number 279 22
Python Program to Count Set Bits in a Number 184 15
Python Program to test Collatz Conjecture for a Given Number 285 18
Python Program to Search the Number of Times a Particular Number Occurs in a List 190 14
Python Program to Determine all Pythagorean Triplets in the Range 211 18
Python Program to Find the Sum of First N Natural Numbers 222 19
Python Program to Find the Sum of Cosine Series 520 18
Python Program to Find the Sum of Sine Series 746 20
Python Program to Check If Two Numbers are Amicable Numbers 189 17
Python Program to Find the LCM of Two Numbers 265 31
Python Program to Check if a Number is a Strong Number 191 20
Python Program to Find Those Numbers which are Divisible by 7 and Multiple of 5 in a Given Range of Numbers 188 17
Python Program to Print Sum of Negative Numbers, Positive Even Numbers and Positive Odd numbers in a List 241 11
Python Program to Compute Prime Factors of an Integer 272 28
Python Program to Check if a Date is Valid and Print the Incremented Date if it is 489 22
Python Program to Print an Inverted Star Pattern 360 20
Python Program to Implement Binary Insertion Sort 183 13
Python Program to Implement Insertion Sort 198 20
Python Program to Implement Binary Search with Recursion 172 19
Python Program to Implement Binary Search without Recursion 245 22
Python Program to Implement Linear Search 230 24
Python Program to Sort using a Binary Search Tree 222 18
Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List 271 26
Python Program to Print an Identity Matrix 181 16
Python Program to Read a Number n and Print the Natural Numbers Summation Pattern 237 26
Python Program to Read a Number n And Print the Series 1+2+.....+n= 241 28
Python Program to Print Odd Numbers Within a Given Range 221 26
Python Program to Accept Three Digits and Print all Possible Combinations from the Digits 344 19
Python Program to Read Two Numbers and Print Their Quotient and Remainder 389 30
Python Program to solve Maximum Subarray Problem using Kadanes Algorithm 298 18
Python Program to Exchange the Values of Two Numbers Without Using a Temporary Variable 199 15
Python Program to Find Element Occurring Odd Number of Times in a List 180 20
Python Program to Replace all Occurrences of a with a $ in a String 206 17
Python Program to Remove the nth Index Character from a Non-Empty String 419 16
Python Program to Check if a Substring is Present in a Given String 242 20
Python Program to Count the Occurrences of Each Word in a Given String Sentence 260 19
Python Program to Form a New String Made of the First 2 and Last 2 characters From a Given String 206 15
Python Program to Calculate the Number of Digits and Letters in a String 311 13
Python Program to Check if a String is a Pangram or Not 263 20
Python Program to Calculate the Number of Upper Case Letters and Lower Case Letters in a String 1044 21
Python Program to Check if a String is a Palindrome or Not 176 11
Python Program to Count Number of Lowercase Characters in a String 204 19
Python Program to Take in Two Strings and Display the Larger String without Using Built-in Functions 227 18
Python Program to Calculate the Number of Words and the Number of Characters Present in a String 211 19
Python Program to Remove the Characters of Odd Index Values in a String 580 23
Python Program to Calculate the Length of a String Without Using a Library Function 209 21
Python Program to Take in a String and Replace Every Blank Space with Hyphen 284 25
Python Program to Count the Number of Vowels in a String 202 23
Python Program to Detect if Two Strings are Anagrams 234 22
Python Program to Form a New String where the First Character and the Last Character have been Exchanged 414 12

Comments