Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
C Program to Check for Palindrome String
Next article icon

C Program To Check If A Linked List Of Strings Forms A Palindrome

Last Updated : 22 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a linked list handling string data, check to see whether data is palindrome or not? Examples:

Input: a -> bc -> d -> dcb -> a -> NULL Output: True String "abcddcba" is palindrome.  Input: a -> bc -> d -> ba -> NULL Output: False String "abcdba" is not palindrome. 
Recommended: Please solve it on "PRACTICE" first, before moving on to the solution.

The idea is very simple. Construct a string out of given linked list and check if the constructed string is palindrome or not. 

C++
// Program to check if a given linked list  // of strings form a palindrome  #include <bits/stdc++.h>  using namespace std;   // Link list node  struct Node  {      string data;      Node* next;  };   // A utility function to check if str  // is palindrome or not  bool isPalindromeUtil(string str)  {      int length = str.length();       // Match characters from beginning      // and end.      for (int i = 0; i < length / 2; i++)          if (str[i] != str[length - i - 1])              return false;       return true;  }   // Returns true if string formed by linked  // list is palindrome  bool isPalindrome(Node *node)  {      // Append all nodes to form a string      string str = "";      while (node != NULL)      {          str.append(node->data);          node = node->next;      }       // Check if the formed string is      // palindrome      return isPalindromeUtil(str);  }   // A utility function to print a given  // linked list  void printList(Node *node)  {      while (node != NULL)      {          cout << node->data << " -> ";          node = node->next;      }      printf("NULL");  }   /* Function to create a new node with  given data */ Node *newNode(const char *str)  {      Node *new_node = new Node;      new_node->data = str;      new_node->next = NULL;      return new_node;  }   // Driver code  int main()  {      Node *head = newNode("a");      head->next = newNode("bc");      head->next->next = newNode("d");      head->next->next->next =      newNode("dcb");      head->next->next->next->next =      newNode("a");      isPalindrome(head)? printf("true"):                          printf("false");      return 0;  }  

Output:

true

Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(m), where m is the length of the string formed by the linked list.

Please refer complete article on Check if a linked list of strings forms a palindrome for more details!


Next Article
C Program to Check for Palindrome String
author
kartik
Improve
Article Tags :
  • Linked List
  • C Programs
  • DSA
  • palindrome
Practice Tags :
  • Linked List
  • palindrome

Similar Reads

  • C Program To Check If A Singly Linked List Is Palindrome
    Given a singly linked list of characters, write a function that returns true if the given list is a palindrome, else false. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. METHOD 1 (By reversing the list): This method takes O(n) time and O(1) extra space. 1) Get t
    7 min read
  • C Program to Check for Palindrome String
    A string is said to be palindrome if the reverse of the string is the same as the string. In this article, we will learn how to check whether the given string is palindrome or not using C program. The simplest method to check for palindrome string is to reverse the given string and store it in a tem
    4 min read
  • Check if a string is palindrome in C using pointers
    Given a string. The task is to check if the string is a palindrome or not using pointers. You are not allowed to use any built-in string functions. A string is said to be a palindrome if the reverse of the string is same as the original string. For example, "madam" is palindrome because when the str
    2 min read
  • C Program to Check if String is Pangram
    Given a string Str. The task is to check if it is Pangram or not.  A pangram is a sentence containing every letter in the English Alphabet. Examples:  Input: "The quick brown fox jumps over the lazy dog" Output: is a Pangram Explanation: Contains all the characters from ‘a’ to ‘z’]  Input: "The quic
    3 min read
  • TCP Client-Server Program to Check if a Given String is Palindrome
    Prerequisites: Socket Programming in C/C++, TCP and UDP server using select, UDP Server-Client implementation in C TCP Client-Server Implementation in C This article describes a Client and Server setup where a Client connects, sends a string to the server and the server shows the original string and
    4 min read
  • C Program To Find Minimum Insertions To Form A Palindrome | DP-28
    Given string str, the task is to find the minimum number of characters to be inserted to convert it to a palindrome. Before we go further, let us understand with a few examples: ab: Number of insertions required is 1 i.e. babaa: Number of insertions required is 0 i.e. aaabcd: Number of insertions re
    9 min read
  • C Program for Longest Palindromic Subsequence | DP-12
    Given a sequence, find the length of the longest palindromic subsequence in it. As another example, if the given sequence is "BBABCBCAB", then the output should be 7 as "BABCBAB" is the longest palindromic subsequence in it. "BBBBB" and "BBCBB" are also palindromic subsequences of the given sequence
    3 min read
  • Check whether the given string is Palindrome using Stack
    Given a string s, the task is to determine whether the given string is a palindrome or not by utilizing a stack. A palindrome is a string that reads the same forwards and backwards. Examples: Input: s = "geeksforgeeks"Output: NoExplanation: Reversing "geeksforgeeks" using a stack results in "skeegro
    10 min read
  • TCS Coding Practice Question | Check Palindrome Number
    Given a number, the task is to check if this number is Palindrome or not using Command Line Arguments . Examples: Input: 123Output: NoInput: 585Output: YesApproach: Since the number is entered as Command line Argument, there is no need for a dedicated input lineExtract the input number from the comm
    4 min read
  • C Program to Compare Two Strings Using Pointers
    In C, two strings are generally compared character by character in lexicographical order (alphabetical order). In this article, we will learn how to compare two strings using pointers. To compare two strings using pointers, increment the pointers to traverse through each character of the strings whi
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences