📌  相关文章
📜  将一个数字添加到以链表形式表示的数字中|套装2

📅  最后修改于: 2021-04-29 05:48:14             🧑  作者: Mango

给定一个单链表,该单链表表示一个数字,其中每个节点仅包含一个数字[0-9] 。任务是在给定的链表所代表的数字上加1并打印新的链表。
例子:

方法:本文讨论了上述问题的先前实现。但是,一种实现方式要求反向链接列表,而另一种则使用递归。这里讨论了O(1)空间复杂度解决方案,该解决方案不需要反向链接列表。
这个问题的主要焦点在数字9上,它会创建所有更改,否则对于其他数字,我们只需要将其值增加1,但是如果我们将节点的值更改为9,则会产生一个进位,然后必须将其传递通过链接列表。
在链接列表中找到不等于9的最后一个节点。现在有三种情况:

  1. 如果没有这样的节点,即每个节点的值为9,则新的链表将包含全0,并在链表的开头插入一个1。
  2. 如果最右边的节点(不等于9)是链表中的最后一个节点,则将该节点加1并返回链表的头。
  3. 如果该节点不是最后一个节点,即其后的每个节点等于9,则将1添加到当前节点,并将其后的所有节点都更改为0。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Node of the linked list
struct Node {
    int data;
    Node* next;
};
 
// Function to create a new node
Node* create_Node(int data)
{
    Node* temp = new Node();
    temp->data = data;
    temp->next = NULL;
    return temp;
}
 
// Function to print the linked list
void print(Node* head)
{
 
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}
 
// Function to add one to a number
// represented as linked list
Node* addOne(Node* head)
{
 
    // To store the last node in the linked
    // list which is not equal to 9
    Node* last = NULL;
    Node* cur = head;
 
    // Iterate till the last node
    while (cur->next != NULL) {
 
        if (cur->data != 9) {
            last = cur;
        }
        cur = cur->next;
    }
 
    // If last node is not equal to 9
    // add 1 to it and return the head
    if (cur->data != 9) {
        cur->data++;
        return head;
    }
 
    // If list is of the type 9 -> 9 -> 9 ...
    if (last == NULL) {
        last = new Node();
        last->data = 0;
        last->next = head;
        head = last;
    }
 
    // For cases when the righmost node which
    // is not equal to 9 is not the last
    // node in the linked list
    last->data++;
    last = last->next;
 
    while (last != NULL) {
        last->data = 0;
        last = last->next;
    }
 
    return head;
}
 
// Driver code
int main()
{
    Node* head = create_Node(1);
    head->next = create_Node(2);
    head->next->next = create_Node(9);
    head->next->next->next = create_Node(9);
 
    cout << "Original list is : ";
    print(head);
 
    head = addOne(head);
 
    cout << "Resultant list is : ";
    print(head);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Node of the linked list
static class Node
{
    int data;
    Node next;
};
 
// Function to create a new node
static Node create_Node(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.next = null;
    return temp;
}
 
// Function to print the linked list
static void print(Node head)
{
    Node temp = head;
    while (temp != null)
    {
        System.out.print(temp.data + " ");
        temp = temp.next;
    }
    System.out.println();
}
 
// Function to add one to a number
// represented as linked list
static Node addOne(Node head)
{
 
    // To store the last node in the linked
    // list which is not equal to 9
    Node last = null;
    Node cur = head;
 
    // Iterate till the last node
    while (cur.next != null)
    {
        if (cur.data != 9)
        {
            last = cur;
        }
        cur = cur.next;
    }
 
    // If last node is not equal to 9
    // add 1 to it and return the head
    if (cur.data != 9)
    {
        cur.data++;
        return head;
    }
 
    // If list is of the type 9 . 9 . 9 ...
    if (last == null)
    {
        last = new Node();
        last.data = 0;
        last.next = head;
        head = last;
    }
 
    // For cases when the righmost node which
    // is not equal to 9 is not the last
    // node in the linked list
    last.data++;
    last = last.next;
 
    while (last != null)
    {
        last.data = 0;
        last = last.next;
    }
    return head;
}
 
// Driver code
public static void main(String[] args)
{
    Node head = create_Node(1);
    head.next = create_Node(2);
    head.next.next = create_Node(9);
    head.next.next.next = create_Node(9);
 
    System.out.print("Original list is : ");
    print(head);
 
    head = addOne(head);
 
    System.out.print("Resultant list is : ");
    print(head);
}
}
 
// This code is contributed
// by PrinciRaj1992


Python3
# A Python3 implementation of the approach
 
# Node of the linked list
class Node():
    def __init__(self):
        self.data = None
        self.next = None
 
# Function to create a new node
def create_Node(data):
    temp = Node()
    temp.data = data
    temp.next = None
    return temp
 
# Function to print the linked list
def printList(head):
    temp = head
    while (temp != None):
        print(temp.data, end = ' ')
        temp = temp.next
    print()
 
# Function to add one to a number
# represented as linked list
def addOne(head):
 
    # To store the last node in the
    # linked list which is not equal to 9
    last = None
    cur = head
 
    # Iterate till the last node
    while(cur.next != None):
        if(cur.data != 9):
            last = cur
        cur = cur.next
 
    # If last node is not equal to 9
    # add 1 to it and return the head
    if(cur.data != 9):
        cur.data += 1
        return head
 
    # If list is of the type 9 -> 9 -> 9 ...
    if(last == None):
        last = Node()
        last.data = 0
        last.next = head
        head = last
 
    # For cases when the rightmost node which
    # is not equal to 9 is not the last
    # node in the linked list
    last.data += 1
    last = last.next
 
    while(last != None):
        last.data = 0
        last = last.next
 
    return head
 
# Driver code
if __name__=='__main__':
    head = create_Node(1)
    head.next = create_Node(2)
    head.next.next = create_Node(9)
    head.next.next.next = create_Node(9)
 
    print("Original list is : ", end = "")
    printList(head)
 
    head = addOne(head)
 
    print("Resultant list is : ", end = "")
    printList(head)
 
# This code is contributed by Yashyasvi Agarwal


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Node of the linked list
public class Node
{
    public int data;
    public Node next;
};
 
// Function to create a new node
static Node create_Node(int data)
{
    Node temp = new Node();
    temp.data = data;
    temp.next = null;
    return temp;
}
 
// Function to print the linked list
static void print(Node head)
{
    Node temp = head;
    while (temp != null)
    {
        Console.Write(temp.data + " ");
        temp = temp.next;
    }
    Console.WriteLine();
}
 
// Function to add one to a number
// represented as linked list
static Node addOne(Node head)
{
 
    // To store the last node in the linked
    // list which is not equal to 9
    Node last = null;
    Node cur = head;
 
    // Iterate till the last node
    while (cur.next != null)
    {
        if (cur.data != 9)
        {
            last = cur;
        }
        cur = cur.next;
    }
 
    // If last node is not equal to 9
    // add 1 to it and return the head
    if (cur.data != 9)
    {
        cur.data++;
        return head;
    }
 
    // If list is of the type 9 . 9 . 9 ...
    if (last == null)
    {
        last = new Node();
        last.data = 0;
        last.next = head;
        head = last;
    }
 
    // For cases when the righmost node which
    // is not equal to 9 is not the last
    // node in the linked list
    last.data++;
    last = last.next;
 
    while (last != null)
    {
        last.data = 0;
        last = last.next;
    }
    return head;
}
 
// Driver code
public static void Main(String[] args)
{
    Node head = create_Node(1);
    head.next = create_Node(2);
    head.next.next = create_Node(9);
    head.next.next.next = create_Node(9);
 
    Console.Write("Original list is : ");
    print(head);
 
    head = addOne(head);
 
    Console.Write("Resultant list is : ");
    print(head);
}
}
 
// This code is contributed by 29AjayKumar


输出:
Original list is : 1 2 9 9 
Resultant list is : 1 3 0 0

时间复杂度: O(N)

空间复杂度: O(1)