C++ Program For Comparing Two Strings Represented As Linked Lists Last Updated : 15 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicographically greater.Examples: Input: list1 = g->e->e->k->s->a list2 = g->e->e->k->s->b Output: -1 Input: list1 = g->e->e->k->s->a list2 = g->e->e->k->s Output: 1 Input: list1 = g->e->e->k->s list2 = g->e->e->k->s Output: 0Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. C++ // C++ program to compare two strings // represented as linked lists #include<bits/stdc++.h> using namespace std; // Linked list Node // structure struct Node { char c; struct Node *next; }; // Function to create newNode // in a linkedlist Node* newNode(char c) { Node *temp = new Node; temp->c = c; temp->next = NULL; return temp; }; int compare(Node *list1, Node *list2) { // Traverse both lists. Stop when // either end of a linked list is // reached or current characters // don't match while (list1 && list2 && list1->c == list2->c) { list1 = list1->next; list2 = list2->next; } // If both lists are not empty, // compare mismatching characters if (list1 && list2) return ((list1->c > list2->c)? 1: -1); // If either of the two lists has // reached end if (list1 && !list2) return 1; if (list2 && !list1) return -1; // If none of the above conditions // is true, both lists have reached // end return 0; } // Driver code int main() { Node *list1 = newNode('g'); list1->next = newNode('e'); list1->next->next = newNode('e'); list1->next->next->next = newNode('k'); list1->next->next->next->next = newNode('s'); list1->next->next->next->next->next = newNode('b'); Node *list2 = newNode('g'); list2->next = newNode('e'); list2->next->next = newNode('e'); list2->next->next->next = newNode('k'); list2->next->next->next->next = newNode('s'); list2->next->next->next->next->next = newNode('a'); cout << compare(list1, list2); return 0; } Output: 1 Time Complexity: O(M + N), where M and N represents the length of the given two linked lists.Auxiliary Space: O(1), no extra space is required, so it is a constant. Please refer complete article on Compare two strings represented as linked lists for more details! Comment More infoAdvertise with us Next Article C++ Program To Check If A Singly Linked List Is Palindrome K kartik Follow Improve Article Tags : Linked List C++ Programs DSA Practice Tags : Linked List Similar Reads C++ Program To Subtract Two Numbers Represented As Linked Lists Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller from the larger ones.It may be assumed that there are no 5 min read C++ Program To Check If Two Linked Lists Are Identical Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE 3 min read C++ Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2- 6 min read C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read 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 (Use a Stack): A simple solution is to use a stack of list nodes. This mainly invol 9 min read C++ Program For Union And Intersection Of Two Linked Lists Write a C++ program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:Int 9 min read Like