Skip to content

Commit b7f23ed

Browse files
refactor 1721
1 parent 35cd0c5 commit b7f23ed

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
return tmp.next;
3131
}
3232
}
33+
3334
public static class Solution2 {
3435
public ListNode swapNodes(ListNode head, int k) {
35-
if(head == null || head.next == null){
36+
if (head == null || head.next == null) {
3637
return head;
3738
}
3839

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

4748
int nums[] = new int[n];
4849
current = head;
4950
int i = 0;
50-
while(current != null){
51+
while (current != null) {
5152
nums[i++] = current.val;
5253
current = current.next;
5354
}
54-
int firstIndex = 0;
55-
int secondIndex = 0;
55+
int firstIndex;
56+
int secondIndex;
5657
firstIndex = k;
57-
secondIndex = n-k;
58-
int temp = nums[firstIndex-1];
59-
nums[firstIndex-1] = nums[secondIndex];
58+
secondIndex = n - k;
59+
int temp = nums[firstIndex - 1];
60+
nums[firstIndex - 1] = nums[secondIndex];
6061
nums[secondIndex] = temp;
6162
ListNode dummy = new ListNode(-1);
6263
current = dummy;
63-
for(i = 0; i<n; i++){
64+
for (i = 0; i < n; i++) {
6465
ListNode node = new ListNode(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
package com.fishercoder;
22

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

89
import static org.junit.Assert.assertEquals;
910

1011
public class _1721Test {
12+
private static _1721.Solution1 solution1;
1113
private static _1721.Solution2 solution2;
1214
private static ListNode expected;
1315
private static ListNode node;
1416
private static int k;
1517

1618
@BeforeClass
1719
public static void setup() {
20+
solution1 = new _1721.Solution1();
1821
solution2 = new _1721.Solution2();
1922
}
2023

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

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

0 commit comments

Comments
 (0)