# Compare numbers represented by Linked Lists

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.

1. If the numbers are equal then print 0.
2. If the number represented by the first linked list is greater then print 1.
3. If the number represented by the second linked list is greater then print -1.

Examples:

Input:
List1 = 2 -> 3 -> 1 -> 2 -> 4 -> NULL
List2 = 2 -> 3 -> 2 -> 4 -> 2 -> NULL
Output: -1

Input:
List1 = 0 -> 0 -> 1 -> 2 -> 4 -> NULL
List2 = 0 -> 0 -> 0 -> 4 -> 2 -> NULL
Output: 1

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `/* Structure for a linked list node */` `struct` `Node { ` `    ``int` `data; ` `    ``struct` `Node* next; ` `}; ` ` `  `/* A helper function to remove zeros from  ` `the start of the linked list */` `Node* removeLeadingZeros(``struct` `Node* a) ` `{ ` `    ``if` `(a != NULL && a->data == 0) ` `        ``return` `removeLeadingZeros(a->next); ` `    ``else` `        ``return` `a; ` `} ` ` `  `/* A hepler function to find the length of  ` `linked list*/` `int` `getSize(``struct` `Node* a) ` `{ ` `    ``int` `sz = 0; ` `    ``while` `(a != NULL) { ` `        ``a = a->next; ` `        ``sz++; ` `    ``} ` `    ``return` `sz; ` `} ` ` `  `/* Given a reference (pointer to pointer) to the  ` `head of a list and an int, push a new node on the  ` `front of the list. */` `void` `push(``struct` `Node** head_ref, ``int` `new_data) ` `{ ` `    ``/* Allocate node */` `    ``struct` `Node* new_node = (``struct` `Node*)``malloc``(``sizeof``(``struct` `Node)); ` ` `  `    ``/* Set the data */` `    ``new_node->data = new_data; ` ` `  `    ``/* Link the old list after the new node */` `    ``new_node->next = (*head_ref); ` ` `  `    ``/* Set the head to point to the new node */` `    ``(*head_ref) = new_node; ` `} ` ` `  `// Function to compar ethe numbers ` `// represented as linked lists ` `int` `compare(``struct` `Node* a, ` `            ``struct` `Node* b) ` `{ ` ` `  `    ``// Remover leading zeroes from ` `    ``// the linked lists ` `    ``a = removeLeadingZeros(a); ` `    ``b = removeLeadingZeros(b); ` ` `  `    ``int` `lenA = getSize(a); ` `    ``int` `lenB = getSize(b); ` ` `  `    ``if` `(lenA > lenB) { ` ` `  `        ``/* Since the number represented by a  ` `        ``has a greater length, it will be greater*/` `        ``return` `1; ` `    ``} ` `    ``else` `if` `(lenB > lenA) { ` `        ``return` `-1; ` `    ``} ` ` `  `    ``/* If the lenghts of two numbers are equal ` `        ``we have to check their magnitudes*/` `    ``while` `(a != NULL && b != NULL) { ` `        ``if` `(a->data > b->data) ` `            ``return` `1; ` `        ``else` `if` `(a->data < b->data) ` `            ``return` `-1; ` ` `  `        ``/* If we reach here, then a and b are  ` `        ``not NULL and their data is same, so  ` `        ``move to next nodes in both lists */` `        ``a = a->next; ` `        ``b = b->next; ` `    ``} ` ` `  `    ``// If linked lists are identical, then ` `    ``// we need to return zero ` `    ``return` `0; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``/* The constructed linked lists are :  ` `    ``a: 5->6->7  ` `    ``b: 2->3->3 */` `    ``struct` `Node* a = NULL; ` `    ``push(&a, 7); ` `    ``push(&a, 6); ` `    ``push(&a, 5); ` ` `  `    ``struct` `Node* b = NULL; ` `    ``push(&b, 3); ` `    ``push(&b, 3); ` `    ``push(&b, 2); ` ` `  `    ``cout << compare(a, b); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` ` `  `/* Structure for a linked list node */` `static` `class` `Node ` `{ ` `    ``int` `data; ` `    ``Node next; ` `}; ` ` `  `/* A helper function to remove zeros from  ` `the start of the linked list */` `static` `Node removeLeadingZeros(Node a) ` `{ ` `    ``if` `(a != ``null` `&& a.data == ``0``) ` `        ``return` `removeLeadingZeros(a.next); ` `    ``else` `        ``return` `a; ` `} ` ` `  `/* A hepler function to find the length of  ` `linked list*/` `static` `int` `getSize(Node a) ` `{ ` `    ``int` `sz = ``0``; ` `    ``while` `(a != ``null``) ` `    ``{ ` `        ``a = a.next; ` `        ``sz++; ` `    ``} ` `    ``return` `sz; ` `} ` ` `  `/* Given a reference (pointer to pointer) to the  ` `head of a list and an int, push a new node on the  ` `front of the list. */` `static` `Node push(Node head_ref, ``int` `new_data) ` `{ ` `    ``/* Allocate node */` `    ``Node new_node = ``new` `Node(); ` ` `  `    ``/* Set the data */` `    ``new_node.data = new_data; ` ` `  `    ``/* Link the old list after the new node */` `    ``new_node.next = head_ref; ` ` `  `    ``/* Set the head to point to the new node */` `    ``head_ref = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `// Function to compar ethe numbers ` `// represented as linked lists ` `static` `int` `compare(Node a, ` `                   ``Node b) ` `{ ` ` `  `    ``// Remover leading zeroes from ` `    ``// the linked lists ` `    ``a = removeLeadingZeros(a); ` `    ``b = removeLeadingZeros(b); ` ` `  `    ``int` `lenA = getSize(a); ` `    ``int` `lenB = getSize(b); ` ` `  `    ``if` `(lenA > lenB)  ` `    ``{ ` ` `  `        ``/* Since the number represented by a  ` `        ``has a greater length, it will be greater*/` `        ``return` `1``; ` `    ``} ` `     `  `    ``else` `if` `(lenB > lenA)  ` `    ``{ ` `        ``return` `-``1``; ` `    ``} ` ` `  `    ``/* If the lenghts of two numbers are equal ` `        ``we have to check their magnitudes*/` `    ``while` `(a != ``null` `&& b != ``null``)  ` `    ``{ ` `        ``if` `(a.data > b.data) ` `            ``return` `1``; ` `        ``else` `if` `(a.data < b.data) ` `            ``return` `-``1``; ` ` `  `        ``/* If we reach here, then a and b are  ` `        ``not null and their data is same, so  ` `        ``move to next nodes in both lists */` `        ``a = a.next; ` `        ``b = b.next; ` `    ``} ` ` `  `    ``// If linked lists are identical, then ` `    ``// we need to return zero ` `    ``return` `0``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `     `  `    ``/* The constructed linked lists are :  ` `    ``a: 5->6->7  ` `    ``b: 2->3->3 */` `    ``Node a = ``null``; ` `    ``a = push(a, ``7``); ` `    ``a = push(a, ``6``); ` `    ``a = push(a, ``5``); ` ` `  `    ``Node b = ``null``; ` `    ``b = push(b, ``3``); ` `    ``b = push(b, ``3``); ` `    ``b = push(b, ``2``); ` ` `  `    ``System.out.println(compare(a, b)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `using` `System.Collections.Generic;              ` ` `  `class` `GFG  ` `{ ` ` `  `/* Structure for a linked list node */` `public` `class` `Node ` `{ ` `    ``public` `int` `data; ` `    ``public` `Node next; ` `}; ` ` `  `/* A helper function to remove zeros from  ` `the start of the linked list */` `static` `Node removeLeadingZeros(Node a) ` `{ ` `    ``if` `(a != ``null` `&& a.data == 0) ` `        ``return` `removeLeadingZeros(a.next); ` `    ``else` `        ``return` `a; ` `} ` ` `  `/* A hepler function to find the length of  ` `linked list*/` `static` `int` `getSize(Node a) ` `{ ` `    ``int` `sz = 0; ` `    ``while` `(a != ``null``) ` `    ``{ ` `        ``a = a.next; ` `        ``sz++; ` `    ``} ` `    ``return` `sz; ` `} ` ` `  `/* Given a reference (pointer to pointer) to the  ` `head of a list and an int, push a new node on the  ` `front of the list. */` `static` `Node push(Node head_ref, ``int` `new_data) ` `{ ` `    ``/* Allocate node */` `    ``Node new_node = ``new` `Node(); ` ` `  `    ``/* Set the data */` `    ``new_node.data = new_data; ` ` `  `    ``/* Link the old list after the new node */` `    ``new_node.next = head_ref; ` ` `  `    ``/* Set the head to point to the new node */` `    ``head_ref = new_node; ` `    ``return` `head_ref; ` `} ` ` `  `// Function to compar ethe numbers ` `// represented as linked lists ` `static` `int` `compare(Node a, Node b) ` `{ ` ` `  `    ``// Remover leading zeroes from ` `    ``// the linked lists ` `    ``a = removeLeadingZeros(a); ` `    ``b = removeLeadingZeros(b); ` ` `  `    ``int` `lenA = getSize(a); ` `    ``int` `lenB = getSize(b); ` ` `  `    ``if` `(lenA > lenB)  ` `    ``{ ` ` `  `        ``/* Since the number represented by a  ` `        ``has a greater length, it will be greater*/` `        ``return` `1; ` `    ``} ` `     `  `    ``else` `if` `(lenB > lenA)  ` `    ``{ ` `        ``return` `-1; ` `    ``} ` ` `  `    ``/* If the lenghts of two numbers are equal ` `        ``we have to check their magnitudes*/` `    ``while` `(a != ``null` `&& b != ``null``)  ` `    ``{ ` `        ``if` `(a.data > b.data) ` `            ``return` `1; ` `        ``else` `if` `(a.data < b.data) ` `            ``return` `-1; ` ` `  `        ``/* If we reach here, then a and b are  ` `        ``not null and their data is same, so  ` `        ``move to next nodes in both lists */` `        ``a = a.next; ` `        ``b = b.next; ` `    ``} ` ` `  `    ``// If linked lists are identical, then ` `    ``// we need to return zero ` `    ``return` `0; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `     `  `    ``/* The constructed linked lists are :  ` `    ``a: 5->6->7  ` `    ``b: 2->3->3 */` `    ``Node a = ``null``; ` `    ``a = push(a, 7); ` `    ``a = push(a, 6); ` `    ``a = push(a, 5); ` ` `  `    ``Node b = ``null``; ` `    ``b = push(b, 3); ` `    ``b = push(b, 3); ` `    ``b = push(b, 2); ` ` `  `    ``Console.WriteLine(compare(a, b)); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```1
```

Time Complexity: O(max(N, M)) where N and M are the lengths of the linked lists.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

Improved By : Rajput-Ji, 29AjayKumar