FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp/data-structures/IncrementLL.cpp at master · AllAlgorithms/cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
AllAlgorithms
/
cpp
Public archive
Notifications
You must be signed in to change notification settings
Fork
340
Star
841
Code
Issues
9
Pull requests
33
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp
/
data-structures
/
IncrementLL.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
127 lines (109 loc) · 1.99 KB
Breadcrumbs
cpp
/
data-structures
/
IncrementLL.cpp
Copy path
File metadata and controls
127 lines (109 loc) · 1.99 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#
include
<
bits/stdc++.h
>
using
namespace
std
;
/*
Increment Linked List by 1
This program increments the list by 1. The program takes input with one space and when entered -1 at the end, it stops taking more inputs
For example
Input:
3 1 -1
Output:
3 2
Input2:
9 9 -1
Output2:
1 0 0
Idea is to reverse a LL, made some calculations and reverse it again to obtain the answer.
*/
class
Node
{
public:
int
data;
Node *next;
Node
(
int
data) {
this
->
data
= data;
this
->
next
=
NULL
;
}
};
Node*
takeInput
() {
Node* head =
NULL
;
Node* prev =
NULL
;
int
d;
cin >> d;
while
(d != -
1
) {
Node* newnode =
new
Node
(d);
if
(head ==
NULL
) {
head = newnode;
prev = head;
}
else
{
prev->
next
= newnode;
prev = newnode;
}
cin >> d;
}
return
head;
}
Node*
reverseLL
(Node* head) {
Node* curr = head;
Node* prev =
NULL
;
while
(curr !=
NULL
) {
if
(prev ==
NULL
) {
prev = curr;
curr = curr->
next
;
prev->
next
=
NULL
;
}
else
{
Node* var = curr;
curr = curr->
next
;
var->
next
= prev;
prev = var;
}
}
return
prev;
}
void
print
(Node* head) {
Node* temp = head;
while
(temp !=
NULL
) {
cout<<temp->
data
<<
"
"
;
temp = temp->
next
;
}
}
int
main
() {
Node* head =
takeInput
();
Node *newHead =
NULL
;
newHead =
reverseLL
(head);
bool
carry =
false
;
Node *temp = newHead;
Node *prev =
NULL
;
int
digit = temp->
data
+
1
;
while
(temp!=
NULL
) {
if
(carry) {
int
data = temp->
data
+
1
;
if
(data >=
10
) {
temp->
data
= (data%
10
);
carry =
true
;
}
else
{
temp->
data
= data;
carry =
false
;
break
;
}
}
else
if
(digit>=
10
) {
temp->
data
= (digit%
10
);
carry =
true
;
}
else
if
(digit<
10
) {
temp->
data
= temp->
data
+
1
;
break
;
}
prev = temp;
temp = temp->
next
;
}
if
(carry) {
Node* newNode =
new
Node
(
1
);
prev->
next
= newNode;
newNode->
next
=
NULL
;
}
head =
reverseLL
(newHead);
print
(head);
}
Back
|
FazBrowse Home
|
New Git URL