Skip to content

Commit 7658906

Browse files
authored
Merge pull request #303 from parmeet9891/master
Incrementing Linked List by one.
2 parents c9d2ef6 + 3a09901 commit 7658906

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

data-structures/IncrementLL.cpp

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
/*
5+
6+
Increment Linked List by 1
7+
8+
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
9+
For example
10+
11+
Input:
12+
3 1 -1
13+
14+
Output:
15+
3 2
16+
17+
Input2:
18+
9 9 -1
19+
20+
Output2:
21+
1 0 0
22+
23+
Idea is to reverse a LL, made some calculations and reverse it again to obtain the answer.
24+
25+
*/
26+
27+
class Node {
28+
public:
29+
int data;
30+
Node *next;
31+
32+
Node(int data) {
33+
this->data = data;
34+
this->next = NULL;
35+
}
36+
};
37+
38+
Node* takeInput() {
39+
Node* head = NULL;
40+
Node* prev = NULL;
41+
int d;
42+
cin >> d;
43+
44+
while(d != -1) {
45+
Node* newnode = new Node(d);
46+
if(head == NULL) {
47+
head = newnode;
48+
prev = head;
49+
}
50+
else {
51+
prev->next = newnode;
52+
prev = newnode;
53+
}
54+
cin >> d;
55+
}
56+
return head;
57+
}
58+
59+
Node* reverseLL(Node* head) {
60+
Node* curr = head;
61+
Node* prev = NULL;
62+
while(curr != NULL) {
63+
if(prev == NULL) {
64+
prev = curr;
65+
curr = curr->next;
66+
prev->next = NULL;
67+
}
68+
else {
69+
Node* var = curr;
70+
curr = curr->next;
71+
var->next = prev;
72+
prev = var;
73+
}
74+
}
75+
return prev;
76+
}
77+
78+
void print(Node* head) {
79+
Node* temp = head;
80+
while(temp != NULL) {
81+
cout<<temp->data<<" ";
82+
temp = temp->next;
83+
}
84+
}
85+
86+
int main() {
87+
Node* head = takeInput();
88+
Node *newHead = NULL;
89+
newHead = reverseLL(head);
90+
91+
bool carry = false;
92+
93+
Node *temp = newHead;
94+
Node *prev = NULL;
95+
int digit = temp->data + 1;
96+
while(temp!= NULL) {
97+
if(carry) {
98+
int data = temp->data + 1;
99+
if(data >= 10) {
100+
temp->data = (data%10);
101+
carry = true;
102+
}
103+
else {
104+
temp->data = data;
105+
carry = false;
106+
break;
107+
}
108+
}
109+
else if(digit>=10) {
110+
temp->data = (digit%10);
111+
carry = true;
112+
}
113+
else if(digit<10) {
114+
temp->data = temp->data + 1;
115+
break;
116+
}
117+
prev = temp;
118+
temp = temp->next;
119+
}
120+
if(carry) {
121+
Node* newNode = new Node(1);
122+
prev->next = newNode;
123+
newNode->next = NULL;
124+
}
125+
head = reverseLL(newHead);
126+
print(head);
127+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy