Python Program For Comparing Two Strings Represented As Linked Lists Last Updated : 15 Jun, 2022 Comments Improve Suggest changes 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. Python # Python program to compare two strings # represented as linked lists # A linked list node # structure class Node: # Constructor to create # a new node def __init__(self, key): self.c = key ; self.next = None def compare(list1, list2): # Traverse both lists. Stop when # either end of linked list is # reached or current characters # don't match while(list1 and list2 and list1.c == list2.c): list1 = list1.next list2 = list2.next # If both lists are not empty, # compare mismatching characters if(list1 and list2): return 1 if list1.c > list2.c else -1 # If either of the two lists has # reached end if (list1 and not list2): return 1 if (list2 and not list1): return -1 return 0 # Driver code list1 = Node('g') list1.next = Node('e') list1.next.next = Node('e') list1.next.next.next = Node('k') list1.next.next.next.next = Node('s') list1.next.next.next.next.next = Node('b') list2 = Node('g') list2.next = Node('e') list2.next.next = Node('e') list2.next.next.next = Node('k') list2.next.next.next.next = Node('s') list2.next.next.next.next.next = Node('a') print compare(list1, list2) # This code is contributed by Nikhil Kumar Singh(nickzuck_007) 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 Python Program For Comparing Two Strings Represented As Linked Lists K kartik Follow Improve Article Tags : Linked List Python Programs DSA Python-DSA Practice Tags : Linked List Similar Reads Python 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 Python program to find String in a List Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e 3 min read Python 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 4 min read Python 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->4->6->8, Ou 4 min read Python Program To Check If A Linked List Of Strings Forms A Palindrome 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. Reco 2 min read Like