Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef8ebbe

Browse files
authored
added one pass solution and test case for 369 (fishercoder1534#155)
1 parent6445bf9 commitef8ebbe

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_369.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,33 @@ public ListNode plusOne(ListNode head) {
5555
}
5656
}
5757

58+
publicstaticclassSolution2 {
59+
60+
publicListNodeplusOne(ListNodehead) {
61+
ListNodedummyNode =newListNode(0);
62+
dummyNode.next =head;
63+
64+
ListNodenotNineNode =dummyNode;
65+
66+
// find the right most node value != 9
67+
while (head !=null) {
68+
if (head.val !=9) {
69+
notNineNode =head;
70+
}
71+
head =head.next;
72+
}
73+
74+
// increase the rightmost node value to 1
75+
notNineNode.val ++;
76+
notNineNode =notNineNode.next;
77+
78+
// set all the following node values with 9 to 0
79+
while (notNineNode !=null) {
80+
notNineNode.val =0;
81+
notNineNode =notNineNode.next;
82+
}
83+
returndummyNode.val !=0 ?dummyNode :dummyNode.next;
84+
}
85+
}
86+
5887
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
5+
importcom.fishercoder.solutions._203;
6+
importcom.fishercoder.solutions._369;
7+
importorg.junit.BeforeClass;
8+
importorg.junit.Test;
9+
10+
importstaticorg.junit.Assert.assertEquals;
11+
12+
publicclass_369Test {
13+
14+
privatestatic_369.Solution2solution2;
15+
privatestaticListNodehead;
16+
privatestaticListNodeexpected;
17+
18+
@BeforeClass
19+
publicstaticvoidsetup() {
20+
solution2 =new_369.Solution2();
21+
}
22+
23+
@Test
24+
publicvoidtest1() {
25+
head =LinkedListUtils.contructLinkedList(newint[]{1,2,9});
26+
expected =LinkedListUtils.contructLinkedList(newint[]{1,3,0});
27+
assertEquals(expected,solution2.plusOne(head));
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp