Skip to content

Commit d53a554

Browse files
committed
feat: add solutions to lc problem: No.0369
1 parent 20d261b commit d53a554

File tree

4 files changed

+148
-6
lines changed

4 files changed

+148
-6
lines changed

solution/0300-0399/0369.Plus One Linked List/README.md

+52-3
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,76 @@
1818
<strong>输出: </strong>[1,2,4]
1919
</pre>
2020

21-
2221
## 解法
2322

2423
<!-- 这里可写通用的实现逻辑 -->
2524

25+
找出链表最右一个 `val ≠ 9` 的节点 target,将 target 值加 1。然后将 target 之后的所有节点值置为 0。
26+
27+
若遇到如 `9 -> 9 -> 9` 的链表,就找不到 target 了,因此,我们可以定义一个虚拟头节点 dummy,初始值为 0。刚开始将 target 指向 dummy,这样就确保链表一定存在一个 `val ≠ 9` 的节点了。
28+
2629
<!-- tabs:start -->
2730

2831
### **Python3**
2932

3033
<!-- 这里可写当前语言的特殊实现逻辑 -->
3134

3235
```python
33-
36+
# Definition for singly-linked list.
37+
# class ListNode:
38+
# def __init__(self, val=0, next=None):
39+
# self.val = val
40+
# self.next = next
41+
class Solution:
42+
def plusOne(self, head: ListNode) -> ListNode:
43+
dummy = ListNode(val=0, next=head)
44+
target = dummy
45+
while head:
46+
if head.val != 9:
47+
target = head
48+
head = head.next
49+
target.val += 1
50+
target = target.next
51+
while target:
52+
target.val = 0
53+
target = target.next
54+
return dummy if dummy.val else dummy.next
3455
```
3556

3657
### **Java**
3758

3859
<!-- 这里可写当前语言的特殊实现逻辑 -->
3960

4061
```java
41-
62+
/**
63+
* Definition for singly-linked list.
64+
* public class ListNode {
65+
* int val;
66+
* ListNode next;
67+
* ListNode() {}
68+
* ListNode(int val) { this.val = val; }
69+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
70+
* }
71+
*/
72+
class Solution {
73+
public ListNode plusOne(ListNode head) {
74+
ListNode dummy = new ListNode(0, head);
75+
ListNode target = dummy;
76+
while (head != null) {
77+
if (head.val != 9) {
78+
target = head;
79+
}
80+
head = head.next;
81+
}
82+
target.val += 1;
83+
target = target.next;
84+
while (target != null) {
85+
target.val = 0;
86+
target = target.next;
87+
}
88+
return dummy.val == 1 ? dummy : dummy.next;
89+
}
90+
}
4291
```
4392

4493
### **...**

solution/0300-0399/0369.Plus One Linked List/README_EN.md

+48-3
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,66 @@
2525
<li>The number represented by the linked list does not contain leading zeros except for the zero itself.&nbsp;</li>
2626
</ul>
2727

28-
2928
## Solutions
3029

3130
<!-- tabs:start -->
3231

3332
### **Python3**
3433

3534
```python
36-
35+
# Definition for singly-linked list.
36+
# class ListNode:
37+
# def __init__(self, val=0, next=None):
38+
# self.val = val
39+
# self.next = next
40+
class Solution:
41+
def plusOne(self, head: ListNode) -> ListNode:
42+
dummy = ListNode(val=0, next=head)
43+
target = dummy
44+
while head:
45+
if head.val != 9:
46+
target = head
47+
head = head.next
48+
target.val += 1
49+
target = target.next
50+
while target:
51+
target.val = 0
52+
target = target.next
53+
return dummy if dummy.val else dummy.next
3754
```
3855

3956
### **Java**
4057

4158
```java
42-
59+
/**
60+
* Definition for singly-linked list.
61+
* public class ListNode {
62+
* int val;
63+
* ListNode next;
64+
* ListNode() {}
65+
* ListNode(int val) { this.val = val; }
66+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
67+
* }
68+
*/
69+
class Solution {
70+
public ListNode plusOne(ListNode head) {
71+
ListNode dummy = new ListNode(0, head);
72+
ListNode target = dummy;
73+
while (head != null) {
74+
if (head.val != 9) {
75+
target = head;
76+
}
77+
head = head.next;
78+
}
79+
target.val += 1;
80+
target = target.next;
81+
while (target != null) {
82+
target.val = 0;
83+
target = target.next;
84+
}
85+
return dummy.val == 1 ? dummy : dummy.next;
86+
}
87+
}
4388
```
4489

4590
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode plusOne(ListNode head) {
13+
ListNode dummy = new ListNode(0, head);
14+
ListNode target = dummy;
15+
while (head != null) {
16+
if (head.val != 9) {
17+
target = head;
18+
}
19+
head = head.next;
20+
}
21+
target.val += 1;
22+
target = target.next;
23+
while (target != null) {
24+
target.val = 0;
25+
target = target.next;
26+
}
27+
return dummy.val == 1 ? dummy : dummy.next;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def plusOne(self, head: ListNode) -> ListNode:
8+
dummy = ListNode(val=0, next=head)
9+
target = dummy
10+
while head:
11+
if head.val != 9:
12+
target = head
13+
head = head.next
14+
target.val += 1
15+
target = target.next
16+
while target:
17+
target.val = 0
18+
target = target.next
19+
return dummy if dummy.val else dummy.next

0 commit comments

Comments
 (0)