Skip to content
Related Articles

Related Articles

Improve Article

Subtract 1 from a number represented as Linked List

  • Difficulty Level : Medium
  • Last Updated : 22 Jun, 2021

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++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Linked list Node
class Node {
public:
    int data;
    Node* next;
};
 
// Function to create a new node with
// the given data
Node* newNode(int data)
{
    // Create a new node
    Node* new_node = new Node;
    new_node->data = data;
    new_node->next = NULL;
 
    // Return the created node
    return new_node;
}
 
// Recursive function to subtract 1
// from the linked list and update
// the node value accordingly
int subtractOneUtil(Node* head)
{
 
    // Base Case
    if (head == NULL)
        return -1;
 
    // Recursively call for the next
    // node of the head
    int borrow = subtractOneUtil(
        head->next);
 
    // If there is a borrow
    if (borrow == -1) {
 
        // If the head data is 0, then
        // update it with 9 and return -1
        if (head->data == 0) {
            head->data = 9;
            return -1;
        }
 
        // Otherwise, decrement head's
        // data by 1 and return 0
        else {
            head->data = head->data - 1;
            return 0;
        }
    }
 
    // Otherwise, return 0
    else {
        return 0;
    }
}
 
// Function to subtract 1 from the given
// Linked List representation of number
Node* subtractOne(Node* head)
{
 
    // Recursively subtract 1 from
    // the Linked List
    subtractOneUtil(head);
 
    // Increment the head pointer
    // if there are any leading zeros
    while (head and head->next
           and head->data == 0) {
        head = head->next;
    }
 
    return head;
}
 
// Function to print a linked list
void printList(Node* node)
{
    // Iterate until node is NULL
    while (node != NULL) {
        cout << node->data;
        node = node->next;
    }
    cout << endl;
}
 
// Driver Code
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




// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Linked list Node
static class Node
{
    int data;
    Node next;
};
 
// Function to create a new node with
// the given data
static Node newNode(int data)
{
     
    // Create a new node
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
 
    // Return the created node
    return new_node;
}
 
// Recursive function to subtract 1
// from the linked list and update
// the node value accordingly
static  int subtractOneUtil(Node head)
{
 
    // Base Case
    if (head == null)
        return -1;
 
    // Recursively call for the next
    // node of the head
    int borrow = subtractOneUtil(
        head.next);
 
    // If there is a borrow
    if (borrow == -1)
    {
         
        // If the head data is 0, then
        // update it with 9 and return -1
        if (head.data == 0)
        {
            head.data = 9;
            return -1;
        }
 
        // Otherwise, decrement head's
        // data by 1 and return 0
        else
        {
            head.data = head.data - 1;
            return 0;
        }
    }
 
    // Otherwise, return 0
    else
    {
        return 0;
    }
}
 
// Function to subtract 1 from the given
// Linked List representation of number
static Node subtractOne(Node head)
{
 
    // Recursively subtract 1 from
    // the Linked List
    subtractOneUtil(head);
 
    // Increment the head pointer
    // if there are any leading zeros
    while (head != null && head.next != null &&
           head.data == 0)
    {
        head = head.next;
    }
    return head;
}
 
// Function to print a linked list
static void printList(Node node)
{
     
    // Iterate until node is null
    while (node != null)
    {
        System.out.print(node.data);
        node = node.next;
    }
    System.out.println();
}
 
// Driver Code
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);
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program for the above approach
 
# Linked list Node
class Node:
     
    def __init__(self, d):
         
        self.data = d
        self.next = None
 
# Recursive function to subtract 1
# from the linked list and update
# the node value accordingly
def subtractOneUtil(head):
     
    # Base Case
    if (head == None):
        return -1
         
    # Recursively call for the next
    # node of the head
    borrow = subtractOneUtil(head.next)
     
    # If there is a borrow
    if (borrow == -1):
         
        # If the head data is 0, then
        # update it with 9 and return -1
        if (head.data == 0):
            head.data = 9
            return -1
             
        # Otherwise, decrement head's
        # data by 1 and return 0
        else:
            head.data = head.data - 1
            return 0
             
    # Otherwise, return 0
    else:
        return 0
 
# Function to subtract 1 from the given
# Linked List representation of number
def subtractOne(head):
 
    # Recursively subtract 1 from
    # the Linked List
    subtractOneUtil(head)
 
    # Increment the head pointer
    # if there are any leading zeros
    while (head and head.next and
           head.data == 0):
        head = head.next
 
    return head
 
# Function to pra linked list
def printList(node):
     
    # Iterate until node is None
    while (node != None):
        print(node.data, end = "")
        node = node.next
         
    print()
 
# Driver Code
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)
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
 
class GFG{
 
// Linked list Node
class Node
{
    public int data;
    public Node next;
};
 
// Function to create a new node with
// the given data
static Node newNode(int data)
{
     
    // Create a new node
    Node new_node = new Node();
    new_node.data = data;
    new_node.next = null;
 
    // Return the created node
    return new_node;
}
 
// Recursive function to subtract 1
// from the linked list and update
// the node value accordingly
static  int subtractOneUtil(Node head)
{
 
    // Base Case
    if (head == null)
        return -1;
 
    // Recursively call for the next
    // node of the head
    int borrow = subtractOneUtil(
        head.next);
 
    // If there is a borrow
    if (borrow == -1)
    {
         
        // If the head data is 0, then
        // update it with 9 and return -1
        if (head.data == 0)
        {
            head.data = 9;
            return -1;
        }
 
        // Otherwise, decrement head's
        // data by 1 and return 0
        else
        {
            head.data = head.data - 1;
            return 0;
        }
    }
 
    // Otherwise, return 0
    else
    {
        return 0;
    }
}
 
// Function to subtract 1 from the given
// Linked List representation of number
static Node subtractOne(Node head)
{
 
    // Recursively subtract 1 from
    // the Linked List
    subtractOneUtil(head);
 
    // Increment the head pointer
    // if there are any leading zeros
    while (head != null && head.next != null &&
           head.data == 0)
    {
        head = head.next;
    }
    return head;
}
 
// Function to print a linked list
static void printList(Node node)
{
     
    // Iterate until node is null
    while (node != null)
    {
        Console.Write(node.data);
        node = node.next;
    }
    Console.WriteLine();
}
 
// Driver Code
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);
}
}
 
// This code is contributed by SURENDRA_GANGWAR

Javascript




<script>
 
// JavaScript program for the above approach
 
// Linked list Node
class Node
{
    constructor()
    {
        this.next = null;
    }
}
 
// Function to create a new node with
// the given data
function newNode(data)
{
     
    // Create a new node
    let new_node = new Node();
    new_node.data = data;
    new_node.next = null;
 
    // Return the created node
    return new_node;
}
 
// Recursive function to subtract 1
// from the linked list and update
// the node value accordingly
function subtractOneUtil(head)
{
     
    // Base Case
    if (head == null)
        return -1;
 
    // Recursively call for the next
    // node of the head
    let borrow = subtractOneUtil(head.next);
 
    // If there is a borrow
    if (borrow == -1)
    {
         
        // If the head data is 0, then
        // update it with 9 and return -1
        if (head.data == 0)
        {
            head.data = 9;
            return -1;
        }
 
        // Otherwise, decrement head's
        // data by 1 and return 0
        else
        {
            head.data = head.data - 1;
            return 0;
        }
    }
 
    // Otherwise, return 0
    else
    {
        return 0;
    }
}
 
// Function to subtract 1 from the given
// Linked List representation of number
function subtractOne(head)
{
     
    // Recursively subtract 1 from
    // the Linked List
    subtractOneUtil(head);
 
    // Increment the head pointer
    // if there are any leading zeros
    while (head != null && head.next != null &&
           head.data == 0)
    {
        head = head.next;
    }
    return head;
}
 
// Function to print a linked list
function printList(node)
{
     
    // Iterate until node is null
    while (node != null)
    {
        document.write(node.data);
        node = node.next;
    }
    document.write("<br>");
}
 
// Driver Code
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);
 
// This code is contributed by Dharanendra L V.
 
</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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :