Python Program For Adding 1 To A Number Represented As Linked List
Last Updated :
29 Mar, 2023
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.
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 ( "Resultant list is: " ,
end = "")
printList(head)
|
Output:
List is 1999
Resultant list is 2000
Time complexity: O(n)
Auxiliary Space: O(1)
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.
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 ("")
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 ( "Resultant list is " )
printList(head)
|
Output:
List is 1999
Resultant list is 2000
Time Complexity: O(n)
Auxiliary Space: O(n)
Please refer complete article on Add 1 to a number represented as linked list for more details!
Share your thoughts in the comments
Please Login to comment...