Skip to content

Commit 59d19a2

Browse files
authored
Add solution and test case for 1721 (#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 parent 1dfaab9 commit 59d19a2

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

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

+35
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,39 @@ public ListNode swapNodes(ListNode head, int k) {
6969
return dummy.next;
7070
}
7171
}
72+
public static class Solution3 {
73+
public ListNode swapNodes(ListNode head, int k) {
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+
int length = 0;
81+
int secondIndex;
82+
83+
ListNode temp1 = null, temp2 = null;
84+
ListNode temp3 = head;
85+
while(temp3 != null){
86+
length++;
87+
temp3 = temp3.next;
88+
}
89+
90+
secondIndex = length - k + 1;
91+
temp3 = head;
92+
for(int i = 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+
int value = temp1.val;
102+
temp1.val = temp2.val;
103+
temp2.val = value;
104+
return head;
105+
}
106+
}
72107
}

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

+23-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
import static org.junit.Assert.assertEquals;
1010

1111
public class _1721Test {
12-
private static _1721.Solution1 solution1;
1312
private static _1721.Solution2 solution2;
13+
private static _1721.Solution3 solution3;
14+
private static _1721.Solution1 solution1;
1415
private static ListNode expected;
1516
private static ListNode node;
1617
private static int k;
@@ -19,21 +20,38 @@ public class _1721Test {
1920
public static void setup() {
2021
solution1 = new _1721.Solution1();
2122
solution2 = new _1721.Solution2();
23+
solution3 = new _1721.Solution3();
2224
}
2325

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

0 commit comments

Comments
 (0)