Skip to content

Commit d47fa0a

Browse files
authored
Merge pull request #681 from geekySapien/geekysapien
Added a C++ solution for add 1 to a number represented by linked list
2 parents 6669d2b + 8db30b9 commit d47fa0a

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
//Link to Problem: https://practice.geeksforgeeks.org/problems/add-1-to-a-number-represented-as-linked-list/1
2+
3+
//Problem Description: We need to add 1 to a number represented as Linked List
4+
5+
//Sample Test Cases
6+
7+
/*
8+
9+
Test Case 1
10+
11+
If the linked list given is as follows
12+
13+
4 -> 5 -> 6
14+
15+
We need to return this linked list as output
16+
17+
4 -> 5 -> 7
18+
19+
*/
20+
21+
/*
22+
23+
Test Case 2
24+
25+
If the linked list given is as follows
26+
27+
9 -> 9 -> 9
28+
29+
We need to return this linked list as output
30+
31+
1 -> 0 -> 0 -> 0
32+
33+
34+
*/
35+
36+
#include <bits/stdc++.h>
37+
using namespace std;
38+
39+
//Structure of Linked List
40+
struct Node
41+
{
42+
int data;
43+
struct Node *next;
44+
45+
Node(int x)
46+
{
47+
data = x;
48+
next = NULL;
49+
}
50+
};
51+
52+
// Function to display a linked list
53+
void printList(Node *node)
54+
{
55+
while (node != NULL)
56+
{
57+
cout << node->data;
58+
node = node->next;
59+
}
60+
cout << "\n";
61+
}
62+
63+
class Solution
64+
{
65+
public:
66+
// A utility function to reverse a linked list
67+
Node *reverse(Node *head)
68+
{
69+
Node *curr = head;
70+
Node *next = NULL;
71+
Node *prev = NULL;
72+
while (curr != NULL)
73+
{
74+
next = curr->next;
75+
curr->next = prev;
76+
prev = curr;
77+
curr = next;
78+
}
79+
return prev;
80+
}
81+
82+
// Function to add 1 to a number represented as linked list
83+
Node *addOne(Node *head)
84+
{
85+
// we first reverse the linked list
86+
head = reverse(head);
87+
88+
// we create a boolean variable whether 1 is added to the number or not.
89+
//If it is true, we assume, we need to add 1 in the number (so we make
90+
//it true by default)
91+
//when 1 is added to the number, we will make it false
92+
bool f = true;
93+
94+
//curr refers to the current pointer which points to the head of the
95+
//linked list
96+
Node *curr = head;
97+
98+
// we iterate till we reach the end of linked list OR we added 1 to the
99+
// linked list OR Both is some case (as described in test case 2)
100+
while (curr != NULL && f == true)
101+
{
102+
103+
//This is the condition that would be executed when we have numbers
104+
//like 9, 99, 999, 9999 and so on
105+
106+
//Here next pointer of current would be null and the data of current
107+
//pointer would be 9
108+
if (curr->next == NULL && curr->data == 9)
109+
{
110+
// we make the data value of current pointer to 1
111+
curr->data = 1;
112+
113+
// we make a new node called temp with value 0 (because we need
114+
// to have 1 extra 0 for example in case of 9 <- 9 <- 9, we make the
115+
// current value to 1 and the list would look like 1 <- 0 <- 0
116+
Node *temp = new Node(0);
117+
118+
// we add that zero before the head, this will make the linked
119+
// list as 1 <- 0 <- 0 <- 0
120+
temp->next = head;
121+
122+
// we make temp as the head of the linked list
123+
head = temp;
124+
125+
//we point current pointer to its next node which will make
126+
//curr point to NULL and will exit from the while loop
127+
curr = curr->next;
128+
}
129+
130+
//This is the case that would be executed when we have numbers like
131+
//19, 119, etc (i.e 9 is present on the LSB(least significant bit) of original number )
132+
else if (curr->data == 9)
133+
{
134+
// we make the value of current pointer to 0 and move to next node
135+
curr->data = 0;
136+
curr = curr->next;
137+
}
138+
139+
//This is the trivial case that would be executed for numbers like 5
140+
//16, 456 etc
141+
else
142+
{
143+
144+
// we increment the value of current's pointer by 1.
145+
curr->data = curr->data + 1;
146+
147+
// we make f false so as to terminate the while loop
148+
f = false;
149+
150+
//move the current pointer to its next node
151+
curr = curr->next;
152+
}
153+
}
154+
155+
//we reverse the reversed linked list to obtain the original linked list
156+
head = reverse(head);
157+
158+
// we finally return the head of new linked list
159+
return head;
160+
}
161+
};
162+
163+
int main()
164+
{
165+
int t;
166+
cin >> t;
167+
while (t--)
168+
{
169+
string s;
170+
cin >> s;
171+
172+
Node *head = new Node(s[0] - '0');
173+
Node *tail = head;
174+
for (int i = 1; i < s.size(); i++)
175+
{
176+
tail->next = new Node(s[i] - '0');
177+
tail = tail->next;
178+
}
179+
Solution ob;
180+
head = ob.addOne(head);
181+
printList(head);
182+
}
183+
return 0;
184+
}

0 commit comments

Comments
 (0)