给定链表的头部代表一个正整数,任务是在从中减去 1 后打印更新的链表。
例子:
Input: LL = 1 -> 2 -> 3 -> 4
Output: 1 -> 2 -> 3 -> 3
Input: LL = 1 -> 2
Output: 1 -> 1
方法:给定的问题可以通过使用递归来解决。请按照以下步骤解决问题:
- 定义一个函数,比如subtractOneUtil(Node *head) ,它将链表的头部作为参数并执行以下步骤:
- 基本情况:如果链表的头节点为NULL ,则从该递归调用中返回-1 。
- 递归调用:递归调用链表的下一个节点,并让这个递归调用返回的值是借位。
- 如果借位的值为-1且头节点的值为0 ,则将头节点的值更新为9并从当前递归调用中返回-1 。
- 否则,将头节点的值减1,并从当前递归调用返回0 。
- 通过将上述函数调用为minusOneUtil(head)从链表中减去1 。
- 如果更新链表有前导0 ,则移动头指针。
- 完成上述步骤后,将更新后的链表打印为结果链表。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出:
List is 1000
Resultant list is 999
时间复杂度: O(N),N 是给定链表的长度。
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live