File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 357
357
366|[ Find Leaves of Binary Tree] ( ./solutions/0366-find-leaves-of-binary-tree.js ) |Medium|
358
358
367|[ Valid Perfect Square] ( ./solutions/0367-valid-perfect-square.js ) |Easy|
359
359
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|
360
361
371|[ Sum of Two Integers] ( ./solutions/0371-sum-of-two-integers.js ) |Medium|
361
362
372|[ Super Pow] ( ./solutions/0372-super-pow.js ) |Medium|
362
363
373|[ Find K Pairs with Smallest Sums] ( ./solutions/0373-find-k-pairs-with-smallest-sums.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments