forked from rakes-git/Hacktoberfest2K21-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem147.cpp
37 lines (37 loc) · 834 Bytes
/
problem147.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
Node* reverse(Node *head){
if(!head||!head->next)
return head;
Node *prev=NULL,*cur=NULL,*nextnode=head;
while(nextnode){
cur = nextnode;
nextnode = nextnode->next;
cur->next = prev;
prev = cur;
}
return prev;
}
Node* addutil(Node *head,int i){
Node* tmp=head;
if(tmp==NULL){
Node* nn = new Node(i);
return nn;
}
if(tmp!=NULL && tmp->data+1<10){
tmp->data = tmp->data+1;
return tmp;
}
if(tmp!=NULL && tmp->data+1==10){
tmp->data = 0;
tmp->next = addutil(tmp->next,1);
return tmp;
}
}
Node* addOne(Node *head)
{
// Your Code here
// return head of list after adding one
Node *hold = reverse(head);
Node *tmp = addutil(hold,1);
head = reverse(hold);
return head;
}