Java 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. Java // Java program to compare two strings // represented as a linked list // Linked List Class class LinkedList { // Head of list Node head; static Node a, b; // Node Class static class Node { char data; Node next; // Constructor to create // a new node Node(char d) { data = d; next = null; } } int compare(Node node1, Node node2) { if (node1 == null && node2 == null) { return 1; } while (node1 != null && node2 != null && node1.data == node2.data) { node1 = node1.next; node2 = node2.next; } // if the list are different // in size if (node1 != null && node2 != null) { return (node1.data > node2.data ? 1 : -1); } // if either of the list has // reached end if (node1 != null && node2 == null) { return 1; } if (node1 == null && node2 != null) { return -1; } return 0; } // Driver code public static void main(String[] args) { LinkedList list = new LinkedList(); Node result = null; list.a = new Node('g'); list.a.next = new Node('e'); list.a.next.next = new Node('e'); list.a.next.next.next = new Node('k'); list.a.next.next.next.next = new Node('s'); list.a.next.next.next.next.next = new Node('b'); list.b = new Node('g'); list.b.next = new Node('e'); list.b.next.next = new Node('e'); list.b.next.next.next = new Node('k'); list.b.next.next.next.next = new Node('s'); list.b.next.next.next.next.next = new Node('a'); int value; value = list.compare(a, b); System.out.println(value); } } // This code is contributed by Mayank Jaiswal 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 Java Program For Comparing Two Strings Represented As Linked Lists kartik Follow Improve Article Tags : Linked List DSA Java Programs Practice Tags : Linked List Similar Reads Java 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 Java Program to Sort LinkedList using Comparable In Java, LinkedList is a part of the collection framework provided in java.util package. LinkedList is a linear data structure where all the elements are unsorted in contiguous memory locations. The advantage of LinkedList is it is dynamic and easy to insert and delete any element. We can not access 5 min read Java Program For Searching An Element In A Linked List Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi 4 min read Java 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- 5 min read Java Program For Swapping Nodes In A Linked List Without Swapping Data Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15- 6 min read Java 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 Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre 5 min read Java 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 Java Program to Search an Element in a Circular Linked List A linked list is a kind of linear data structure where each node has a data part and an address part which points to the next node. A circular linked list is a type of linked list where the last node points to the first one, making a circle of nodes. Example: Input : CList = 6->5->4->3-> 3 min read Java Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 2->1->2->1->1->2->0->1->0 Output: 0->0->1->1->1->1->2->2->2 The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2. Input: 2->1->0 Output: 0->1->2 The sorted Array is 0, 1, 2Recommended: Please solve it on "PRACTICE" first, before moving on to th 3 min read Like