-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdd_1_linkedList.cpp
More file actions
42 lines (42 loc) · 869 Bytes
/
Add_1_linkedList.cpp
File metadata and controls
42 lines (42 loc) · 869 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* plusOne(ListNode* head) {
if(head==NULL)
return head;
ListNode* node=head;
ListNode* start=NULL;
while(node)
{
if(node->val<9)
start=node;
node=node->next;
}
if(start)
{
start->val=start->val+1;
node=start->next;
}
else if(!start)
{
ListNode* n = new ListNode(1);
n->next=head;
head=n;
node=head->next;
//head=n;
}
while(node)
{
node->val=0;
node=node->next;
}
return head;
}
};