Given the pointers to the head nodes of two linked lists. The task is to compare the numbers represented by the linked lists. The numbers represented by the lists may contain leading zeros.
- If the numbers are equal then print 0.
- If the number represented by the first linked list is greater then print 1.
- If the number represented by the second linked list is greater then print -1.
List1 = 2 -> 3 -> 1 -> 2 -> 4 -> NULL
List2 = 2 -> 3 -> 2 -> 4 -> 2 -> NULL
List1 = 0 -> 0 -> 1 -> 2 -> 4 -> NULL
List2 = 0 -> 0 -> 0 -> 4 -> 2 -> NULL
Approach: Since the numbers may contain leading zeros, first remove all the leading zeros from the start of the linked lists. After that compare their lengths, if the lengths are unequal, this means that one of the numbers is definitely greater and return 1 or -1 based upon whose length is greater. Else traverse both the lists simultaneously, and while traversing we compare the digits at every node. If at any point, the digit is unequal then return either 1 or -1 based on the value of the digits. If the end of the linked lists is reached then the linked lists are identical and hence return 0.
Below is the implementation of the above approach:
Time Complexity: O(max(N, M)) where N and M are the lengths of the linked lists.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Compare two strings represented as linked lists
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Add Two Numbers Represented by Linked Lists | Set 3
- Multiply two numbers represented by Linked Lists
- Subtract Two Numbers represented as Linked Lists
- Multiply two numbers represented as linked lists into a third list
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Add 1 to a number represented as linked list
- Add one to a number represented as linked list | Set 2
- Reverse all the word in a String represented as a Linked List
- Find two Fibonacci numbers whose sum can be represented as N
- Multiply Large Numbers represented as Strings
- Sum of two numbers where one number is represented as array of digits
- Check if a number can be represented as a sum of 2 triangular numbers
- Count numbers which can be represented as sum of same parity primes
- Check if given number can be represented as sum of two great numbers
- Modulo power for large numbers represented as strings
- Identical Linked Lists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.