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

Commitb7f23ed

Browse files
refactor 1721
1 parent35cd0c5 commitb7f23ed

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,38 @@ public ListNode swapNodes(ListNode head, int k) {
3030
returntmp.next;
3131
}
3232
}
33+
3334
publicstaticclassSolution2 {
3435
publicListNodeswapNodes(ListNodehead,intk) {
35-
if(head ==null ||head.next ==null){
36+
if(head ==null ||head.next ==null){
3637
returnhead;
3738
}
3839

3940
// find length of list
4041
intn =0;
4142
ListNodecurrent =head;
42-
while(current !=null){
43+
while(current !=null){
4344
current =current.next;
4445
n++;
4546
}
4647

4748
intnums[] =newint[n];
4849
current =head;
4950
inti =0;
50-
while(current !=null){
51+
while(current !=null){
5152
nums[i++] =current.val;
5253
current =current.next;
5354
}
54-
intfirstIndex =0;
55-
intsecondIndex =0;
55+
intfirstIndex;
56+
intsecondIndex;
5657
firstIndex =k;
57-
secondIndex =n-k;
58-
inttemp =nums[firstIndex-1];
59-
nums[firstIndex-1] =nums[secondIndex];
58+
secondIndex =n -k;
59+
inttemp =nums[firstIndex -1];
60+
nums[firstIndex -1] =nums[secondIndex];
6061
nums[secondIndex] =temp;
6162
ListNodedummy =newListNode(-1);
6263
current =dummy;
63-
for(i =0;i<n;i++){
64+
for(i =0;i <n;i++){
6465
ListNodenode =newListNode(nums[i]);
6566
current.next =node;
6667
current =current.next;

‎src/test/java/com/fishercoder/_1721Test.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.common.classes.ListNode;
4+
importcom.fishercoder.common.utils.LinkedListUtils;
45
importcom.fishercoder.solutions._1721;
56
importorg.junit.BeforeClass;
67
importorg.junit.Test;
78

89
importstaticorg.junit.Assert.assertEquals;
910

1011
publicclass_1721Test {
12+
privatestatic_1721.Solution1solution1;
1113
privatestatic_1721.Solution2solution2;
1214
privatestaticListNodeexpected;
1315
privatestaticListNodenode;
1416
privatestaticintk;
1517

1618
@BeforeClass
1719
publicstaticvoidsetup() {
20+
solution1 =new_1721.Solution1();
1821
solution2 =new_1721.Solution2();
1922
}
2023

2124
@Test
2225
publicvoidtest1() {
23-
node =newListNode(1);
24-
node.next =newListNode(2);
25-
node.next.next =newListNode(3);
26-
node.next.next.next =newListNode(4);
27-
node.next.next.next.next =newListNode(5);
28-
29-
expected =newListNode(1);
30-
expected.next =newListNode(4);
31-
expected.next.next =newListNode(3);
32-
expected.next.next.next =newListNode(2);
33-
expected.next.next.next.next =newListNode(5);
26+
node =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
27+
expected =LinkedListUtils.contructLinkedList(newint[]{1,4,3,2,5});
28+
k =2;
29+
assertEquals(expected,solution1.swapNodes(node,k));
30+
}
3431

32+
@Test
33+
publicvoidtest2() {
34+
node =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
35+
expected =LinkedListUtils.contructLinkedList(newint[]{1,4,3,2,5});
3536
k =2;
3637
assertEquals(expected,solution2.swapNodes(node,k));
3738
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp