Skip to content

Commit d240061

Browse files
committed
Add solution #369
1 parent f7d749d commit d240061

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@
357357
366|[Find Leaves of Binary Tree](./solutions/0366-find-leaves-of-binary-tree.js)|Medium|
358358
367|[Valid Perfect Square](./solutions/0367-valid-perfect-square.js)|Easy|
359359
368|[Largest Divisible Subset](./solutions/0368-largest-divisible-subset.js)|Medium|
360+
369|[Plus One Linked List](./solutions/0369-plus-one-linked-list.js)|Medium|
360361
371|[Sum of Two Integers](./solutions/0371-sum-of-two-integers.js)|Medium|
361362
372|[Super Pow](./solutions/0372-super-pow.js)|Medium|
362363
373|[Find K Pairs with Smallest Sums](./solutions/0373-find-k-pairs-with-smallest-sums.js)|Medium|
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* 369. Plus One Linked List
3+
* https://leetcode.com/problems/plus-one-linked-list/
4+
* Difficulty: Medium
5+
*
6+
* Given a non-negative integer represented as a linked list of digits, plus one to the integer.
7+
*
8+
* The digits are stored such that the most significant digit is at the head of the list.
9+
*/
10+
11+
/**
12+
* Definition for singly-linked list.
13+
* function ListNode(val, next) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.next = (next===undefined ? null : next)
16+
* }
17+
*/
18+
/**
19+
* @param {ListNode} head
20+
* @return {ListNode}
21+
*/
22+
var plusOne = function(head) {
23+
const list = new ListNode(0, head);
24+
let notNine = list;
25+
26+
while (head) {
27+
if (head.val !== 9) notNine = head;
28+
head = head.next;
29+
}
30+
31+
notNine.val++;
32+
notNine = notNine.next;
33+
34+
while (notNine) {
35+
notNine.val = 0;
36+
notNine = notNine.next;
37+
}
38+
39+
return list.val === 0 ? list.next : list;
40+
};

0 commit comments

Comments
 (0)