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

Commit59d19a2

Browse files
authored
Add solution and test case for 1721 (fishercoder1534#158)
* added solution and test case for 1721* merged with master* removed comments and initialized variables on new lines* add solution and testcase for 1721
1 parent1dfaab9 commit59d19a2

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,39 @@ public ListNode swapNodes(ListNode head, int k) {
6969
returndummy.next;
7070
}
7171
}
72+
publicstaticclassSolution3 {
73+
publicListNodeswapNodes(ListNodehead,intk) {
74+
// O(n) linear time
75+
/*
76+
1. Calculate length of linked list
77+
2. Initialize 3 ptrs, temp1 and temp2 used for pointing to nodes at k, (len - k + 1)
78+
and temp3 used to iterate over the linked list
79+
*/
80+
intlength =0;
81+
intsecondIndex;
82+
83+
ListNodetemp1 =null,temp2 =null;
84+
ListNodetemp3 =head;
85+
while(temp3 !=null){
86+
length++;
87+
temp3 =temp3.next;
88+
}
89+
90+
secondIndex =length -k +1;
91+
temp3 =head;
92+
for(inti =1;i <=length;i++){
93+
if(i ==k){
94+
temp1 =temp3;
95+
}
96+
if(i ==secondIndex){
97+
temp2 =temp3;
98+
}
99+
temp3 =temp3.next;
100+
}
101+
intvalue =temp1.val;
102+
temp1.val =temp2.val;
103+
temp2.val =value;
104+
returnhead;
105+
}
106+
}
72107
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
importstaticorg.junit.Assert.assertEquals;
1010

1111
publicclass_1721Test {
12-
privatestatic_1721.Solution1solution1;
1312
privatestatic_1721.Solution2solution2;
13+
privatestatic_1721.Solution3solution3;
14+
privatestatic_1721.Solution1solution1;
1415
privatestaticListNodeexpected;
1516
privatestaticListNodenode;
1617
privatestaticintk;
@@ -19,21 +20,38 @@ public class _1721Test {
1920
publicstaticvoidsetup() {
2021
solution1 =new_1721.Solution1();
2122
solution2 =new_1721.Solution2();
23+
solution3 =new_1721.Solution3();
2224
}
2325

2426
@Test
2527
publicvoidtest1() {
26-
node =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
27-
expected =LinkedListUtils.contructLinkedList(newint[]{1,4,3,2,5});
28+
node =newListNode(1);
29+
node.next =newListNode(2);
30+
node.next.next =newListNode(3);
31+
node.next.next.next =newListNode(4);
32+
node.next.next.next.next =newListNode(5);
33+
34+
expected =newListNode(1);
35+
expected.next =newListNode(4);
36+
expected.next.next =newListNode(3);
37+
expected.next.next.next =newListNode(2);
38+
expected.next.next.next.next =newListNode(5);
39+
2840
k =2;
29-
assertEquals(expected,solution1.swapNodes(node,k));
41+
assertEquals(expected,solution2.swapNodes(node,k));
3042
}
31-
3243
@Test
3344
publicvoidtest2() {
3445
node =LinkedListUtils.contructLinkedList(newint[]{1,2,3,4,5});
3546
expected =LinkedListUtils.contructLinkedList(newint[]{1,4,3,2,5});
3647
k =2;
3748
assertEquals(expected,solution2.swapNodes(node,k));
3849
}
50+
@Test
51+
publicvoidtest3(){
52+
node =LinkedListUtils.contructLinkedList(newint[]{90,100});
53+
k =2;
54+
expected =LinkedListUtils.contructLinkedList(newint[]{100,90});
55+
assertEquals(expected,solution3.swapNodes(node,k));
56+
}
3957
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp