Compare numbers represented by Linked Lists
Last Updated :
19 Dec, 2022
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.
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
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 returns 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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* removeLeadingZeros( struct Node* a)
{
if (a != NULL && a->data == 0)
return removeLeadingZeros(a->next);
else
return a;
}
int getSize( struct Node* a)
{
int sz = 0;
while (a != NULL) {
a = a->next;
sz++;
}
return sz;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = ( struct Node*) malloc ( sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int compare( struct Node* a,
struct Node* b)
{
a = removeLeadingZeros(a);
b = removeLeadingZeros(b);
int lenA = getSize(a);
int lenB = getSize(b);
if (lenA > lenB) {
return 1;
}
else if (lenB > lenA) {
return -1;
}
while (a != NULL && b != NULL) {
if (a->data > b->data)
return 1;
else if (a->data < b->data)
return -1;
a = a->next;
b = b->next;
}
return 0;
}
int main()
{
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
class GFG
{
static class Node
{
int data;
Node next;
};
static Node removeLeadingZeros(Node a)
{
if (a != null && a.data == 0 )
return removeLeadingZeros(a.next);
else
return a;
}
static int getSize(Node a)
{
int sz = 0 ;
while (a != null )
{
a = a.next;
sz++;
}
return sz;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int compare(Node a,
Node b)
{
a = removeLeadingZeros(a);
b = removeLeadingZeros(b);
int lenA = getSize(a);
int lenB = getSize(b);
if (lenA > lenB)
{
return 1 ;
}
else if (lenB > lenA)
{
return - 1 ;
}
while (a != null && b != null )
{
if (a.data > b.data)
return 1 ;
else if (a.data < b.data)
return - 1 ;
a = a.next;
b = b.next;
}
return 0 ;
}
public static void main(String[] args)
{
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));
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def removeLeadingZeros(a):
if (a ! = None and a.data = = 0 ):
return removeLeadingZeros(a. next );
else :
return a;
def getSize(a):
sz = 0 ;
while (a ! = None ):
a = a. next ;
sz + = 1
return sz;
def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data;
new_node. next = (head_ref);
(head_ref) = new_node;
return head_ref
def compare(a, b):
a = removeLeadingZeros(a);
b = removeLeadingZeros(b);
lenA = getSize(a);
lenB = getSize(b);
if (lenA > lenB):
return 1 ;
elif (lenB > lenA):
return - 1 ;
while (a ! = None and b ! = None ):
if (a.data > b.data):
return 1 ;
elif (a.data < b.data):
return - 1 ;
a = a. next ;
b = b. next ;
return 0 ;
if __name__ = = '__main__' :
a = None ;
a = push(a, 7 );
a = push(a, 6 );
a = push(a, 5 );
b = None ;
b = push(b, 3 );
b = push(b, 3 );
b = push(b, 2 );
print (compare(a, b))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node removeLeadingZeros(Node a)
{
if (a != null && a.data == 0)
return removeLeadingZeros(a.next);
else
return a;
}
static int getSize(Node a)
{
int sz = 0;
while (a != null )
{
a = a.next;
sz++;
}
return sz;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static int compare(Node a, Node b)
{
a = removeLeadingZeros(a);
b = removeLeadingZeros(b);
int lenA = getSize(a);
int lenB = getSize(b);
if (lenA > lenB)
{
return 1;
}
else if (lenB > lenA)
{
return -1;
}
while (a != null && b != null )
{
if (a.data > b.data)
return 1;
else if (a.data < b.data)
return -1;
a = a.next;
b = b.next;
}
return 0;
}
public static void Main(String[] args)
{
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));
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function removeLeadingZeros( a)
{
if (a != null && a.data == 0)
return removeLeadingZeros(a.next);
else
return a;
}
function getSize( a)
{
let sz = 0;
while (a != null )
{
a = a.next;
sz++;
}
return sz;
}
function push( head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function compare( a, b)
{
a = removeLeadingZeros(a);
b = removeLeadingZeros(b);
let lenA = getSize(a);
let lenB = getSize(b);
if (lenA > lenB)
{
return 1;
}
else if (lenB > lenA)
{
return -1;
}
while (a != null && b != null )
{
if (a.data > b.data)
return 1;
else if (a.data < b.data)
return -1;
a = a.next;
b = b.next;
}
return 0;
}
var a = null ;
a = push(a, 7);
a = push(a, 6);
a = push(a, 5);
var b = null ;
b = push(b, 3);
b = push(b, 3);
b = push(b, 2);
document.write(compare(a, b));
</script>
|
Time Complexity: O(max(N, M)) where N and M are the lengths of the linked lists.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...