Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0)
Below are the steps :
- Reverse given linked list. For example, 1-> 9-> 9 -> 9 is converted to 9-> 9 -> 9 ->1.
- Start traversing linked list from leftmost node and add 1 to it. If there is a carry, move to the next node. Keep moving to the next node while there is a carry.
- Reverse modified linked list and return head.
Below is the implementation of above steps.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node* next;
};
Node *newNode( int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
Node *reverse(Node *head)
{
Node * prev = NULL;
Node * current = head;
Node * next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
Node *addOneUtil(Node *head)
{
Node* res = head;
Node *temp;
int carry = 1, sum;
while (head != NULL)
{
sum = carry + head->data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head->data = sum;
temp = head;
head = head->next;
}
if (carry > 0)
temp->next = newNode(carry);
return res;
}
Node* addOne(Node *head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
void printList(Node *node)
{
while (node != NULL)
{
cout << node->data;
node = node->next;
}
cout<<endl;
}
int main( void )
{
Node *head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
cout << "List is " ;
printList(head);
head = addOne(head);
cout << "\nResultant list is " ;
printList(head);
return 0;
}
|
C
#include<bits/stdc++.h>
struct Node
{
int data;
Node* next;
};
Node *newNode( int data)
{
Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
Node *reverse(Node *head)
{
Node * prev = NULL;
Node * current = head;
Node * next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
Node *addOneUtil(Node *head)
{
Node* res = head;
Node *temp, *prev = NULL;
int carry = 1, sum;
while (head != NULL)
{
sum = carry + head->data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head->data = sum;
temp = head;
head = head->next;
}
if (carry > 0)
temp->next = newNode(carry);
return res;
}
Node* addOne(Node *head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
void printList(Node *node)
{
while (node != NULL)
{
printf ( "%d" , node->data);
node = node->next;
}
printf ( "\n" );
}
int main( void )
{
Node *head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
printf ( "List is " );
printList(head);
head = addOne(head);
printf ( "\nResultant list is " );
printList(head);
return 0;
}
|
Java
class GfG {
static class Node {
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverse(Node head)
{
Node prev = null ;
Node current = head;
Node next = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
static Node addOneUtil(Node head)
{
Node res = head;
Node temp = null , prev = null ;
int carry = 1 , sum;
while (head != null )
{
sum = carry + head.data;
carry = (sum >= 10 ) ? 1 : 0 ;
sum = sum % 10 ;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0 )
temp.next = newNode(carry);
return res;
}
static Node addOne(Node head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
static void printList(Node node)
{
while (node != null ) {
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python3
import sys
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(data):
return Node(data)
def reverseList(head):
if not head:
return
curNode = head
prevNode = head
nextNode = head. next
curNode. next = None
while (nextNode):
curNode = nextNode
nextNode = nextNode. next
curNode. next = prevNode
prevNode = curNode
return curNode
def addOne(head):
head = reverseList(head)
k = head
carry = 0
prev = None
head.data + = 1
while (head ! = None ) and (head.data > 9 or carry > 0 ):
prev = head
head.data + = carry
carry = head.data / / 10
head.data = head.data % 10
head = head. next
if carry > 0 :
prev. next = Node(carry)
return reverseList(k)
def printList(head):
if not head:
return
while (head):
print ( "{}" . format (head.data), end = "")
head = head. next
if __name__ = = '__main__' :
head = newNode( 1 )
head. next = newNode( 9 )
head. next . next = newNode( 9 )
head. next . next . next = newNode( 9 )
print ( "List is: " , end = "")
printList(head)
head = addOne(head)
print ( "\nResultant list is: " , end = "")
printList(head)
|
C#
using System;
class GfG {
public class Node {
public int data;
public Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverse(Node head)
{
Node prev = null ;
Node current = head;
Node next = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
static Node addOneUtil(Node head)
{
Node res = head;
Node temp = null , prev = null ;
int carry = 1, sum;
while (head != null )
{
sum = carry + head.data;
carry = (sum >= 10) ? 1 : 0;
sum = sum % 10;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0)
temp.next = newNode(carry);
return res;
}
static Node addOne(Node head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
static void printList(Node node)
{
while (node != null ) {
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
Node head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
Console.Write( "List is " );
printList(head);
head = addOne(head);
Console.WriteLine();
Console.Write( "Resultant list is " );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function newNode(data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function reverse(head)
{
var prev = null ;
var current = head;
var next;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
function addOneUtil(head)
{
var res = head;
var temp, prev = null ;
var carry = 1, sum;
while (head != null )
{
sum = carry + head.data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0)
temp.next = newNode(carry);
return res;
}
function addOne(head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
function printList(node)
{
while (node != null )
{
document.write( node.data);
node = node.next;
}
document.write( "<br>" );
}
var head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
document.write( "List is " );
printList(head);
head = addOne(head);
document.write( "<br>Resultant list is " );
printList(head);
</script>
|
Output
List is 1999
Resultant list is 2000
Time Complexity: O(n), Here n is the number of nodes in the linked list.
Auxiliary Space: O(1), As constant extra space is used.
Recursive Implementation:
We can recursively reach the last node and forward carry to previous nodes. Recursive solution doesn’t require reversing of linked list. We can also use a stack in place of recursion to temporarily hold nodes.
Below is the implementation of recursive solution.
C++
#include <bits/stdc++.h>
struct Node {
int data;
Node* next;
};
Node* newNode( int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
int addWithCarry(Node* head)
{
if (head == NULL)
return 1;
int res = head->data + addWithCarry(head->next);
head->data = (res) % 10;
return (res) / 10;
}
Node* addOne(Node* head)
{
int carry = addWithCarry(head);
if (carry) {
Node* newNode = new Node;
newNode->data = carry;
newNode->next = head;
return newNode;
}
return head;
}
void printList(Node* node)
{
while (node != NULL) {
printf ( "%d" , node->data);
node = node->next;
}
printf ( "\n" );
}
int main( void )
{
Node* head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
printf ( "List is " );
printList(head);
head = addOne(head);
printf ( "\nResultant list is " );
printList(head);
return 0;
}
|
Java
class GfG {
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static int addWithCarry(Node head)
{
if (head == null )
return 1 ;
int res = head.data + addWithCarry(head.next);
head.data = (res) % 10 ;
return (res) / 10 ;
}
static Node addOne(Node head)
{
int carry = addWithCarry(head);
if (carry > 0 )
{
Node newNode = newNode(carry);
newNode.next = head;
return newNode;
}
return head;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(data):
new_node = Node( 0 )
new_node.data = data
new_node. next = None
return new_node
def addWithCarry(head):
if (head = = None ):
return 1
res = head.data + addWithCarry(head. next )
head.data = int ((res) % 10 )
return int ((res) / 10 )
def addOne(head):
carry = addWithCarry(head)
if (carry ! = 0 ):
newNode = Node( 0 )
newNode.data = carry
newNode. next = head
return newNode
return head
def printList(node):
while (node ! = None ):
print ( node.data,end = "")
node = node. next
print ( "\n" )
head = newNode( 1 )
head. next = newNode( 9 )
head. next . next = newNode( 9 )
head. next . next . next = newNode( 9 )
print ( "List is " )
printList(head)
head = addOne(head)
print ( "\nResultant list is " )
printList(head)
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
public static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
public static int addWithCarry(Node head)
{
if (head == null )
return 1;
int res = head.data + addWithCarry(head.next);
head.data = (res) % 10;
return (res) / 10;
}
public static Node addOne(Node head)
{
int carry = addWithCarry(head);
Node newNodes = null ;
if (carry > 0)
{
newNodes = newNode(carry);
newNodes.next = head;
return newNodes;
}
return head;
}
public static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
Node head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
Console.Write( "List is " );
printList(head);
head = addOne(head);
Console.WriteLine();
Console.Write( "Resultant list is " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function newNode(data) {
var new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function addWithCarry(head) {
if (head == null ) return 1;
var res = head.data + addWithCarry(head.next);
head.data = res % 10;
return parseInt(res / 10);
}
function addOne(head) {
var carry = addWithCarry(head);
var newNodes = null ;
if (carry > 0) {
newNodes = newNode(carry);
newNodes.next = head;
return newNodes;
}
return head;
}
function printList(node) {
while (node != null ) {
document.write(node.data);
node = node.next;
}
document.write( "<br>" );
}
var head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
document.write( "List is " );
printList(head);
head = addOne(head);
document.write( "<br>" );
document.write( "Resultant list is " );
printList(head);
</script>
|
Output
List is 1999
Resultant list is 2000
Time Complexity: O(n), Here n is the number of nodes in the linked list.
Auxiliary Space: O(n),The extra space is used in recursion call stack.
Simple approach and easy implementation: The idea is to store the last non 9 digit pointer so that if the last pointer is zero we can replace all the nodes after stored node(which contains the location of last digit before 9) to 0 and add the value of the stored node by 1
C++
#include <bits/stdc++.h>
struct Node {
int data;
Node* next;
};
Node* newNode( int data)
{
Node* new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
}
Node* addOne(Node* head)
{
Node* ln = head;
if (head->next == NULL) {
head->data += 1;
return head;
}
Node* t = head;
int prev;
while (t->next) {
if (t->data != 9) {
ln = t;
}
t = t->next;
}
if (t->data == 9 && ln != NULL) {
if (ln->data == 9 && ln == head) {
Node* temp = newNode(1);
temp->next = head;
head = temp;
t = ln;
}
else {
t = ln;
t->data += 1;
t = t->next;
}
while (t) {
t->data = 0;
t = t->next;
}
}
else {
t->data += 1;
}
return head;
}
void printList(Node* node)
{
while (node != NULL) {
printf ( "%d->" , node->data);
node = node->next;
}
printf ( "NULL" );
printf ( "\n" );
}
int main( void )
{
Node* head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
printf ( "List is " );
printList(head);
head = addOne(head);
printf ( "\nResultant list is " );
printList(head);
return 0;
}
|
Java
class GFG{
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node addOne(Node head)
{
Node ln = head;
if (head.next == null )
{
head.data += 1 ;
return head;
}
Node t = head;
int prev;
while (t.next != null )
{
if (t.data != 9 )
{
ln = t;
}
t = t.next;
}
if (t.data == 9 && ln != null )
{
t = ln;
t.data += 1 ;
t = t.next;
while (t != null )
{
t.data = 0 ;
t = t.next;
}
}
else
{
t.data += 1 ;
}
return head;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python3
class GFG :
class Node :
data = 0
next = None
@staticmethod
def newNode( data) :
new_node = GFG.Node()
new_node.data = data
new_node. next = None
return new_node
@staticmethod
def addOne( head) :
ln = head
if (head. next = = None ) :
head.data + = 1
return head
t = head
prev = 0
while (t. next ! = None ) :
if (t.data ! = 9 ) :
ln = t
t = t. next
if (t.data = = 9 and ln ! = None ) :
t = ln
t.data + = 1
t = t. next
while (t ! = None ) :
t.data = 0
t = t. next
else :
t.data + = 1
return head
@staticmethod
def printList( node) :
while (node ! = None ) :
print (node.data, end = "")
node = node. next
print ()
@staticmethod
def main( args) :
head = GFG.newNode( 1 )
head. next = GFG.newNode( 9 )
head. next . next = GFG.newNode( 9 )
head. next . next . next = GFG.newNode( 9 )
print ( "List is " , end = "")
GFG.printList(head)
head = GFG.addOne(head)
print ()
print ( "Resultant list is " , end = "")
GFG.printList(head)
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
class GfG {
public class Node {
public int data;
public Node next;
}
public static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
public static Node addOne(Node head)
{
Node ln = head;
if (head.next == null ) {
head.data += 1;
return head;
}
Node t = head;
while (t.next != null ) {
if (t.data != 9) {
ln = t;
}
t = t.next;
}
if (t.data == 9 && ln != null ) {
t = ln;
t.data += 1;
t = t.next;
while (t != null ) {
t.data = 0;
t = t.next;
}
}
else {
t.data += 1;
}
return head;
}
public static void printList(Node node)
{
while (node != null ) {
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
Node head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
Console.Write( "List is " );
printList(head);
head = addOne(head);
Console.WriteLine();
Console.Write( "Resultant list is " );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .next = null ;
this .data = 0;
}
}
function newNode(data)
{
let new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function addOne(head)
{
let ln = head;
if (head.next == null )
{
head.data += 1;
return head;
}
let t = head;
let prev;
while (t.next != null )
{
if (t.data != 9)
{
ln = t;
}
t = t.next;
}
if (t.data == 9 && ln != null )
{
t = ln;
t.data += 1;
t = t.next;
while (t != null )
{
t.data = 0;
t = t.next;
}
}
else
{
t.data += 1;
}
return head;
}
function printList(node)
{
while (node != null )
{
document.write(node.data);
node = node.next;
}
document.write( "</br>" );
}
let head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
document.write( "List is " );
printList(head);
head = addOne(head);
document.write( "</br>" );
document.write( "Resultant list is " );
printList(head);
</script>
|
Output
List is 1999
Resultant list is 2000
Time Complexity: O(n), Here n is the number of nodes in the linked list.
Auxiliary Space: O(1), As constant extra space is used.
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
The easy way to add 1 in number represented by linked list.
The Approach:
we first traverse over lined list and get the number that there in linked list simple while loop code and then add one to that number and then convert that number to linked list by simple while loop of(%10 and /10).
C++
#include <bits/stdc++.h>
#include<iostream>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* newNode( int data)
{
Node* new_node = (Node*) malloc ( sizeof (Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = newNode(new_data);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* n)
{
while (n) {
cout << n->data << " " ;
n = n->next;
}
cout << endl;
}
Node* addTwoLists(Node*first){
int num1=0;
while (first->next!=NULL){
num1 +=first->data;
num1*=10;
first=first->next;
}
num1+=first->data;
int num2=num1+1;
Node* temp=NULL;
while (num2!=0){
int last=num2%10;
push(&temp,last);
num2=num2/10;
}
return temp;
}
int main() {
Node* first = NULL;
push(&first, 6);
push(&first, 4);
push(&first, 9);
push(&first, 5);
push(&first, 7);
Node* ans = addTwoLists(first);
cout << "Sum is of first : " ;
printList(ans);
Node* second=NULL;
push(&second, 9);
push(&second, 9);
push(&second, 9);
push(&second, 1);
cout << "Sum is of second : " ;
Node* res=addTwoLists(second);
printList(res);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node next;
Node( int d) {
data = d;
next = null ;
}
}
class AddLinkedList
{
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList(Node n) {
while (n != null ) {
System.out.print(n.data + " " );
n = n.next;
}
System.out.println();
}
static Node addTwoLists(Node first) {
int num1 = 0 ;
while (first.next != null ) {
num1 += first.data;
num1 *= 10 ;
first = first.next;
}
num1 += first.data;
int num2 = num1 + 1 ;
Node temp = null ;
while (num2 != 0 ) {
int last = num2 % 10 ;
temp = push(temp, last);
num2 = num2 / 10 ;
}
return temp;
}
public static void main(String[] args) {
Node first = null ;
first = push(first, 6 );
first = push(first, 4 );
first = push(first, 9 );
first = push(first, 5 );
first = push(first, 7 );
Node ans = addTwoLists(first);
System.out.print( "Sum is of first : " );
printList(ans);
Node second = null ;
second = push(second, 9 );
second = push(second, 9 );
second = push(second, 9 );
second = push(second, 1 );
System.out.print( "Sum is of second : " );
Node res = addTwoLists(second);
printList(res);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def push(head_ref, new_data):
new_node = Node(new_data)
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(n):
while n:
print (n.data, end = " " )
n = n. next
print ()
def addTwoLists(first):
num1 = 0
while first. next ! = None :
num1 + = first.data
num1 * = 10
first = first. next
num1 + = first.data
num2 = num1 + 1
temp = None
while num2 ! = 0 :
last = num2 % 10
temp = push(temp, last)
num2 / / = 10
return temp
if __name__ = = '__main__' :
first = None
first = push(first, 6 )
first = push(first, 4 )
first = push(first, 9 )
first = push(first, 5 )
first = push(first, 7 )
ans = addTwoLists(first)
print ( "Sum is of first: " , end = "")
printList(ans)
second = None
second = push(second, 9 )
second = push(second, 9 )
second = push(second, 9 )
second = push(second, 1 )
res = addTwoLists(second)
print ( "Sum is of second: " , end = "")
printList(res)
|
C#
using System;
class Node {
public int data;
public Node next;
public Node( int d) {
data = d;
next = null ;
}
}
class AddLinkedList {
static Node push(Node head_ref, int new_data) {
Node new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
static void printList(Node n) {
while (n != null ) {
Console.Write(n.data + " " );
n = n.next;
}
Console.WriteLine();
}
static Node addTwoLists(Node first) {
int num1 = 0;
while (first.next != null ) {
num1 += first.data;
num1 *= 10;
first = first.next;
}
num1 += first.data;
int num2 = num1 + 1;
Node temp = null ;
while (num2 != 0) {
int last = num2 % 10;
temp = push(temp, last);
num2 = num2 / 10;
}
return temp;
}
static void Main( string [] args) {
Node first = null ;
first = push(first, 6);
first = push(first, 4);
first = push(first, 9);
first = push(first, 5);
first = push(first, 7);
Node ans = addTwoLists(first);
Console.Write( "Sum is of first : " );
printList(ans);
Node second = null ;
second = push(second, 9);
second = push(second, 9);
second = push(second, 9);
second = push(second, 1);
Console.Write( "Sum is of second : " );
Node res = addTwoLists(second);
printList(res);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function push(head_ref, new_data) {
const new_node = new Node(new_data);
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
function printList(n) {
let ans= "" ;
while (n) {
ans = ans + n.data+ " " ;
n = n.next;
}
console.log(ans);
}
function addTwoLists(first) {
let num1 = 0;
while (first.next != null ) {
num1 += first.data;
num1 *= 10;
first = first.next;
}
num1 += first.data;
let num2 = num1 + 1;
let temp = null ;
while (num2 != 0) {
const last = num2 % 10;
temp = push(temp, last);
num2 = Math.floor(num2 / 10);
}
return temp;
}
let first = null ;
first = push(first, 6);
first = push(first, 4);
first = push(first, 9);
first = push(first, 5);
first = push(first, 7);
let ans = addTwoLists(first);
console.log( "Sum is of first: " );
printList(ans);
let second = null ;
second = push(second, 9);
second = push(second, 9);
second = push(second, 9);
second = push(second, 1);
let res = addTwoLists(second);
console.log( "Sum is of second: " );
printList(res);
|
Time Complexity: O(n), Here n is the number of nodes in the linked list.
Auxiliary Space: O(n), Need one temporary linked list to store result.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!