forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add1-linked_list.cpp
75 lines (57 loc) · 1.46 KB
/
add1-linked_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// C++ program to add one to a linked list
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Recursively add 1 from end to beginning and return
// carry after all nodes are processed.
int addWithCarry(Node *head) {
// If linked list is empty, return carry
if (head == nullptr) {
return 1;
}
// Add carry returned by the next node call
int res = head->data + addWithCarry(head->next);
// Update data and return new carry
head->data = res % 10;
return res / 10;
}
Node *addOne(Node *head) {
// Add 1 to linked list from end to beginning
int carry = addWithCarry(head);
// If there is carry after updating all nodes,
// then we need to add a new node to the linked list
if (carry) {
Node *newNode = new Node(carry);
newNode->next = head;
// New node becomes head now
return newNode;
}
return head;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hard-coded linked list:
// 1 -> 9 -> 9 -> 9
Node *head = new Node(1);
head->next = new Node(9);
head->next->next = new Node(9);
head->next->next->next = new Node(9);
head = addOne(head);
printList(head);
return 0;
}