Given the head of the linked list representing a positive integer, the task is to print the updated linked list after subtracting 1 from it.
Examples:
Input: LL = 1 -> 2 -> 3 -> 4
Output: 1 -> 2 -> 3 -> 3
Input: LL = 1 -> 2
Output: 1 -> 1
Approach: The given problem can be solved by using recursion. Follow the steps below to solve the problem:
- Define a function, say subtractOneUtil(Node *head) that takes the head of the linked list as the arguments and perform the following steps:
- Base Case: If the head node of the Linked List is NULL, then return -1 from that recursive call.
- Recursive Call: Recursively call for the next node of the linked list and let the value returned by this recursive call be borrow.
- If the value of borrow is -1 and the value of the head node is 0, then update the value of the head node to 9 and return -1 from the current recursive call.
- Otherwise, decrement the value of the head node by 1 and return 0 from the current recursive call.
- Subtract 1 from the Linked List by calling the above function as subtractOneUtil(head).
- If the update linked list has leading 0s, then move the head pointer.
- After completing the above steps, print the updated linked list as the resultant linked list.
Below is the implementation of the above approach:
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;
}
int subtractOneUtil(Node* head)
{
if (head == NULL)
return -1;
int borrow = subtractOneUtil(
head->next);
if (borrow == -1) {
if (head->data == 0) {
head->data = 9;
return -1;
}
else {
head->data = head->data - 1;
return 0;
}
}
else {
return 0;
}
}
Node* subtractOne(Node* head)
{
subtractOneUtil(head);
while (head and head->next
and head->data == 0) {
head = head->next;
}
return head;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(0);
head->next->next = newNode(0);
head->next->next->next = newNode(0);
cout << "List is " ;
printList(head);
head = subtractOne(head);
cout << "Resultant list is " ;
printList(head);
return 0;
}
|
Java
import java.util.*;
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 subtractOneUtil(Node head)
{
if (head == null )
return - 1 ;
int borrow = subtractOneUtil(
head.next);
if (borrow == - 1 )
{
if (head.data == 0 )
{
head.data = 9 ;
return - 1 ;
}
else
{
head.data = head.data - 1 ;
return 0 ;
}
}
else
{
return 0 ;
}
}
static Node subtractOne(Node head)
{
subtractOneUtil(head);
while (head != null && head.next != null &&
head.data == 0 )
{
head = head.next;
}
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( 0 );
head.next.next = newNode( 0 );
head.next.next.next = newNode( 0 );
System.out.print( "List is " );
printList(head);
head = subtractOne(head);
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , d):
self .data = d
self . next = None
def subtractOneUtil(head):
if (head = = None ):
return - 1
borrow = subtractOneUtil(head. next )
if (borrow = = - 1 ):
if (head.data = = 0 ):
head.data = 9
return - 1
else :
head.data = head.data - 1
return 0
else :
return 0
def subtractOne(head):
subtractOneUtil(head)
while (head and head. next and
head.data = = 0 ):
head = head. next
return head
def printList(node):
while (node ! = None ):
print (node.data, end = "")
node = node. next
print ()
if __name__ = = '__main__' :
head = Node( 1 )
head. next = Node( 0 )
head. next . next = Node( 0 )
head. next . next . next = Node( 0 )
print ( "List is " , end = "")
printList(head)
head = subtractOne(head)
print ( "Resultant list is " , end = "")
printList(head)
|
C#
using System;
class GFG{
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 int subtractOneUtil(Node head)
{
if (head == null )
return -1;
int borrow = subtractOneUtil(
head.next);
if (borrow == -1)
{
if (head.data == 0)
{
head.data = 9;
return -1;
}
else
{
head.data = head.data - 1;
return 0;
}
}
else
{
return 0;
}
}
static Node subtractOne(Node head)
{
subtractOneUtil(head);
while (head != null && head.next != null &&
head.data == 0)
{
head = head.next;
}
return head;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
public static void Main()
{
Node head = newNode(1);
head.next = newNode(0);
head.next.next = newNode(0);
head.next.next.next = newNode(0);
Console.Write( "List is " );
printList(head);
head = subtractOne(head);
Console.Write( "Resultant list is " );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .next = null ;
}
}
function newNode(data)
{
let new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function subtractOneUtil(head)
{
if (head == null )
return -1;
let borrow = subtractOneUtil(head.next);
if (borrow == -1)
{
if (head.data == 0)
{
head.data = 9;
return -1;
}
else
{
head.data = head.data - 1;
return 0;
}
}
else
{
return 0;
}
}
function subtractOne(head)
{
subtractOneUtil(head);
while (head != null && head.next != null &&
head.data == 0)
{
head = head.next;
}
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(0);
head.next.next = newNode(0);
head.next.next.next = newNode(0);
document.write( "List is " );
printList(head);
head = subtractOne(head);
document.write( "Resultant list is " );
printList(head);
</script>
|
Output
List is 1000
Resultant list is 999
Time Complexity: O(N), N is the length of the given linked list.
Auxiliary Space: O(1)
New Approach by reverse the linked list :
- Subtract 1 from the first node (i.e., the ones place), and then handle the carry if require.
- .Then, reverse the linked list back to its original form and return the updated linked list.
Below is the implementation of the above approach:
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* reverseList(Node* head)
{
Node *prev = NULL, *curr = head, *next;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
Node* subtractOne(Node* head)
{
head = reverseList(head);
head->data -= 1;
Node* curr = head;
while (curr->data < 0) {
curr->data += 10;
if (curr->next == NULL) {
curr->next = newNode(0);
}
curr->next->data -= 1;
curr = curr->next;
}
head = reverseList(head);
while (head and head->next and head->data == 0) {
head = head->next;
}
return head;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(0);
head->next->next = newNode(0);
head->next->next->next = newNode(0);
cout << "List is " ;
printList(head);
head = subtractOne(head);
cout << "Resultant list is " ;
printList(head);
return 0;
}
|
Java
public class Main {
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 reverseList(Node head) {
Node prev = null , curr = head, next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
static Node subtractOne(Node head) {
head = reverseList(head);
head.data -= 1 ;
Node curr = head;
while (curr.data < 0 ) {
curr.data += 10 ;
if (curr.next == null ) {
curr.next = newNode( 0 );
}
curr.next.data -= 1 ;
curr = curr.next;
}
head = reverseList(head);
while (head != null && head.next != null && head.data == 0 ) {
head = head.next;
}
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( 0 );
head.next.next = newNode( 0 );
head.next.next.next = newNode( 0 );
System.out.print( "List is " );
printList(head);
head = subtractOne(head);
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def newNode(data):
new_node = Node(data)
return new_node
def reverseList(head):
prev = None
curr = head
next = None
while curr ! = None :
next = curr. next
curr. next = prev
prev = curr
curr = next
return prev
def subtractOne(head):
head = reverseList(head)
head.data - = 1
curr = head
while curr.data < 0 :
curr.data + = 10
if curr. next = = None :
curr. next = newNode( 0 )
curr. next .data - = 1
curr = curr. next
head = reverseList(head)
while head and head. next and head.data = = 0 :
head = head. next
return head
def printList(node):
while node ! = None :
print (node.data, end = "")
node = node. next
print ()
head = newNode( 1 )
head. next = newNode( 0 )
head. next . next = newNode( 0 )
head. next . next . next = newNode( 0 )
print ( "List is " )
printList(head)
head = subtractOne(head)
print ( "Resultant list is " )
printList(head)
|
C#
using System;
class Node {
public int data;
public Node next;
};
class MainClass {
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverseList(Node head)
{
Node prev = null , curr = head, next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
static Node subtractOne(Node head)
{
head = reverseList(head);
head.data -= 1;
Node curr = head;
while (curr.data < 0) {
curr.data += 10;
if (curr.next == null ) {
curr.next = newNode(0);
}
curr.next.data -= 1;
curr = curr.next;
}
head = reverseList(head);
while (head != null && head.next != null
&& head.data == 0) {
head = head.next;
}
return head;
}
static void printList(Node node)
{
while (node != null ) {
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
public static void Main()
{
Node head = newNode(1);
head.next = newNode(0);
head.next.next = newNode(0);
head.next.next.next = newNode(0);
Console.Write( "List is " );
printList(head);
head = subtractOne(head);
Console.Write( "Resultant list is " );
printList(head);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function newNode(data)
{
let new_node = new Node(data);
return new_node;
}
function reverseList(head) {
let prev = null ,
curr = head,
next;
while (curr != null ) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
function subtractOne(head) {
head = reverseList(head);
head.data -= 1;
let curr = head;
while (curr.data < 0) {
curr.data += 10;
if (curr.next == null ) {
curr.next = newNode(0);
}
curr.next.data -= 1;
curr = curr.next;
}
head = reverseList(head);
while (head && head.next && head.data == 0) {
head = head.next;
}
return head;
}
function printList(node) {
while (node != null ) {
process.stdout.write(node.data + "" );
node = node.next;
}
console.log();
}
let head = newNode(1);
head.next = newNode(0);
head.next.next = newNode(0);
head.next.next.next = newNode(0);
console.log( "List is " );
printList(head);
head = subtractOne(head);
console.log( "Resultant list is " );
printList(head);
|
Output
List is 1000
Resultant list is 999
Time Complexity: O(n), where n is the length of the linked list.
Space Complexity: O(n), where n is the length of the linked list.
Another approach using Stack:
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* subtractOne(Node* head)
{
stack< int > s;
Node* curr = head;
while (curr != NULL) {
s.push(curr->data);
curr = curr->next;
}
int borrow = 1;
Node* new_head = NULL;
while (!s.empty()) {
int val = s.top() - borrow;
borrow = 0;
if (val < 0) {
val += 10;
borrow = 1;
}
s.pop();
Node* new_node = newNode(val);
new_node->next = new_head;
new_head = new_node;
}
while (new_head != NULL && new_head->data == 0) {
Node* temp = new_head;
new_head = new_head->next;
delete temp;
}
return new_head;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data;
node = node->next;
}
cout << endl;
}
int main()
{
Node* head = newNode(1);
head->next = newNode(0);
head->next->next = newNode(0);
head->next->next->next = newNode(0);
cout << "List is " ;
printList(head);
head = subtractOne(head);
cout << "Resultant list is " ;
printList(head);
return 0;
}
|
Java
import java.util.Stack;
class Node {
int data;
Node next;
Node( int data)
{
this .data = data;
this .next = null ;
}
}
public class Main {
static Node subtractOne(Node head)
{
Stack<Integer> s = new Stack<>();
Node curr = head;
while (curr != null ) {
s.push(curr.data);
curr = curr.next;
}
int borrow = 1 ;
Node new_head = null ;
while (!s.empty()) {
int val = s.pop() - borrow;
borrow = 0 ;
if (val < 0 ) {
val += 10 ;
borrow = 1 ;
}
Node new_node = new Node(val);
new_node.next = new_head;
new_head = new_node;
}
while (new_head != null && new_head.data == 0 ) {
Node temp = new_head;
new_head = new_head.next;
temp = null ;
}
return new_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 = new Node( 1 );
head.next = new Node( 0 );
head.next.next = new Node( 0 );
head.next.next.next = new Node( 0 );
System.out.print( "List is " );
printList(head);
head = subtractOne(head);
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def new_node(data):
new_node = Node(data)
return new_node
def subtract_one(head):
stack = []
curr = head
while curr is not None :
stack.append(curr.data)
curr = curr. next
borrow = 1
new_head = None
while stack:
val = stack.pop() - borrow
borrow = 0
if val < 0 :
val + = 10
borrow = 1
new_node = Node(val)
new_node. next = new_head
new_head = new_node
while new_head is not None and new_head.data = = 0 :
temp = new_head
new_head = new_head. next
del temp
return new_head
def print_list(node):
while node is not None :
print node.data,
node = node. next
print
if __name__ = = "__main__" :
head = new_node( 1 )
head. next = new_node( 0 )
head. next . next = new_node( 0 )
head. next . next . next = new_node( 0 )
print "List is " ,
print_list(head)
head = subtract_one(head)
print "Resultant list is " ,
print_list(head)
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
}
class GFG {
static Node NewNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node SubtractOne(Node head)
{
Stack< int > stack = new Stack< int >();
Node curr = head;
while (curr != null ) {
stack.Push(curr.data);
curr = curr.next;
}
int borrow = 1;
Node new_head = null ;
while (stack.Count > 0) {
int val = stack.Pop() - borrow;
borrow = 0;
if (val < 0) {
val += 10;
borrow = 1;
}
Node new_node = NewNode(val);
new_node.next = new_head;
new_head = new_node;
}
while (new_head != null && new_head.data == 0) {
new_head = new_head.next;
}
return new_head;
}
static void PrintList(Node node)
{
while (node != null ) {
Console.Write(node.data);
node = node.next;
}
Console.WriteLine();
}
static void Main()
{
Node head = NewNode(1);
head.next = NewNode(0);
head.next.next = NewNode(0);
head.next.next.next = NewNode(0);
Console.Write( "List is " );
PrintList(head);
head = SubtractOne(head);
Console.Write( "Resultant list is " );
PrintList(head);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
function new_node(data) {
const new_node = new Node(data);
return new_node;
}
function subtract_one(head) {
const stack = [];
let curr = head;
while (curr !== null ) {
stack.push(curr.data);
curr = curr.next;
}
let borrow = 1;
let new_head = null ;
while (stack.length > 0) {
let val = stack.pop() - borrow;
borrow = 0;
if (val < 0) {
val += 10;
borrow = 1;
}
const new_node = new Node(val);
new_node.next = new_head;
new_head = new_node;
}
while (new_head !== null && new_head.data === 0) {
const temp = new_head;
new_head = new_head.next;
}
return new_head;
}
function print_list(node) {
while (node !== null ) {
console.log(node.data);
node = node.next;
}
}
const head = new_node(1);
head.next = new_node(0);
head.next.next = new_node(0);
head.next.next.next = new_node(0);
console.log( "List is:" );
print_list(head);
const newHead = subtract_one(head);
console.log( "Resultant list is:" );
print_list(newHead);
|
Output
List is 1000
Resultant list is 999
Time Complexity: O(n)
Space Complexity: O(n)
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!