-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut103.c++
111 lines (94 loc) · 1.76 KB
/
tut103.c++
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
/*Add 1 to a number represented as linked list -> A number N is represented in Linked List such that each digit corresponds to a node in linked list. You need to add 1 to it.
Example 1:
Input:
LinkedList: 4->5->6
Output: 4->5->7
Example 2:
Input:
LinkedList: 1->2->3
Output: 1->2->4
*/
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *next;
// Node(int data)
// {
// this->data = data;
// this->next = NULL;
// }
};
Node *newNode(int data)
{
Node *temp = new Node();
temp->data = data;
temp->next = NULL;
return temp;
}
void printLinkedList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
Node *addOne(Node *head)
{
Node *curr = head;
Node *last = head;
int flag = 0, cnt = 0;
while (curr)
{
if (curr->data != 9)
{
last = curr;
cnt++;
}
else
{
flag = 1;
}
curr = curr->next;
}
if (flag == 0)
{
last->data++;
return head;
}
if (cnt != 0)
{
last->data++;
last = last->next;
while (last)
{
last->data = 0;
last = last->next;
}
return head;
}
while (last)
{
last->data = 0;
last = last->next;
}
Node *t = newNode(1);
t->next = head;
head = t;
return head;
}
int main()
{
Node *head = newNode(4);
head->next = newNode(5);
head->next->next = newNode(7);
cout << "Linked list before adding 1\n";
printLinkedList(head);
head = addOne(head);
cout << "\nLinked list after adding 1\n";
printLinkedList(head);
return 0;
}