📌  相关文章
📜  将 1 添加到表示为链表的数字

📅  最后修改于: 2022-05-13 01:57:44.087000             🧑  作者: Mango

将 1 添加到表示为链表的数字

数字用链表表示,每个数字对应链表中的一个节点。加1。例如 1999 表示为 (1-> 9-> 9 -> 9) 并且向其添加 1 应将其更改为 (2->0->0->0)

以下是步骤:

  1. 反转给定的链表。例如,1-> 9-> 9 -> 9 转换为 9-> 9 -> 9 -> 1。
  2. 从最左边的节点开始遍历链表并加1。如果有进位,移动到下一个节点。有进位时继续移动到下一个节点。
  3. 反向修改链表并返回头部。

下面是上述步骤的实现。

C++


Java


Python3


C#


Javascript


C++


Java


Python


C#


Javascript


C++
// Recursive C++ program to add 1 to a linked list#include /* Linked list node */struct Node {int data;Node* next;};/* Function to create a new node with given data */Node* newNode(int data){Node* new_node = new Node;new_node->data = data;new_node->next = NULL;return new_node;}Node* addOne(Node* head){// Your Code here// return head of list after adding oneNode* ln = head;if (head->next == NULL) {head->data += 1;return head;}Node* t = head;int prev;while (t->next) {if (t->data != 9) {ln = t;}t = t->next;}if (t->data == 9 && ln != NULL) {if (ln->data == 9 && ln == head) {Node* temp = newNode(1);temp->next = head;head = temp;t = ln;}else {t = ln;t->data += 1;t = t->next;}while (t) {t->data = 0;t = t->next;}}else {t->data += 1;}return head;}// A utility function to print a linked listvoid printList(Node* node){while (node != NULL) {printf(“%d->”, node->data);node = node->next;}printf(“NULL”);printf(“\n”);}/* Driver code */int main(void){Node* head = newNode(1);head->next = newNode(9);head->next->next = newNode(9);head->next->next->next = newNode(9);printf(“List is “);printList(head);
                          var adpushup = window.adpushup  = window.adpushup || {que:[]};
                          adpushup.que.push(function() {
                            adpushup.triggerAd("1b4172cd-acc7-4363-9184-3fadd3cdabcf");
                          });
                        head = addOne(head);printf(“\nResultant list is “);printList(head);return 0;}// This code is contribute bu maddler


Java
// Recursive Java program to add 1 to a linked listclass GFG{// Linked list nodestatic class Node{int data;Node next;}// Function to create a new node with given datastatic Node newNode(int data){Node new_node = new Node();new_node.data = data;new_node.next = null;return new_node;}static Node addOne(Node head){// Return head of list after adding oneNode ln = head;if (head.next == null){head.data += 1;return head;}Node t = head;int prev;while (t.next != null){if (t.data != 9){ln = t;}t = t.next;}if (t.data == 9 && ln != null){t = ln;t.data += 1;t = t.next;while (t != null){t.data = 0;t = t.next;}}else{t.data += 1;}return head;}// A utility function to print a linked liststatic void printList(Node node){while (node != null){System.out.print(node.data);node = node.next;}System.out.println();}// Driver codepublic static void main(String[] args){Node head = newNode(1);head.next = newNode(9);head.next.next = newNode(9);head.next.next.next = newNode(9);System.out.print(“List is “);printList(head);head = addOne(head);System.out.println();System.out.print(“Resultant list is “);printList(head);}}// This code is contributed by rajsanghavi9.


输出
List is 1999

Resultant list is 2000


递归实现:
我们可以递归地到达最后一个节点并将进位转发到前一个节点。递归解决方案不需要反转链表。我们还可以使用堆栈代替递归来临时保存节点。

下面是递归解决方案的实现。



C++

Java

Python

C#

Javascript