From b4800fc2c5afb4f4bf4721509e1e533b1eac537b Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Sun, 10 Jan 2021 23:07:38 -0700 Subject: [PATCH 1/4] added solution and test case for 1721 --- src/main/java/com/fishercoder/solutions/_1721.java | 4 ++++ src/test/java/com/fishercoder/_1721Test.java | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/com/fishercoder/solutions/_1721.java create mode 100644 src/test/java/com/fishercoder/_1721Test.java diff --git a/src/main/java/com/fishercoder/solutions/_1721.java b/src/main/java/com/fishercoder/solutions/_1721.java new file mode 100644 index 0000000000..f128a39bd9 --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_1721.java @@ -0,0 +1,4 @@ +package com.fishercoder.solutions; + +public class _1721 { +} diff --git a/src/test/java/com/fishercoder/_1721Test.java b/src/test/java/com/fishercoder/_1721Test.java new file mode 100644 index 0000000000..78fce7dea4 --- /dev/null +++ b/src/test/java/com/fishercoder/_1721Test.java @@ -0,0 +1,4 @@ +package com.fishercoder; + +public class _1721Test { +} From 2e7364cb76d8903637ba41dd4bafe2c27fbbc280 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Sun, 10 Jan 2021 23:12:45 -0700 Subject: [PATCH 2/4] merged with master --- .../java/com/fishercoder/solutions/_1721.java | 63 +++++++++++++++++++ src/test/java/com/fishercoder/_1721Test.java | 34 ++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/main/java/com/fishercoder/solutions/_1721.java b/src/main/java/com/fishercoder/solutions/_1721.java index f128a39bd9..1a2d27e92e 100644 --- a/src/main/java/com/fishercoder/solutions/_1721.java +++ b/src/main/java/com/fishercoder/solutions/_1721.java @@ -1,4 +1,67 @@ package com.fishercoder.solutions; +import com.fishercoder.common.classes.ListNode; + +import java.util.ArrayList; +import java.util.List; + public class _1721 { + public static class Solution1 { + public ListNode swapNodes(ListNode head, int k) { + List list = new ArrayList<>(); + ListNode tmp = head; + while (tmp != null) { + list.add(tmp.val); + tmp = tmp.next; + } + int first = list.get(k - 1); + int size = list.size(); + int second = list.get(size - k); + list.remove(k - 1); + list.add(k - 1, second); + list.remove(size - k); + list.add(size - k, first); + ListNode pre = new ListNode(-1); + tmp = pre; + for (int i = 0; i < list.size(); i++) { + pre.next = new ListNode(list.get(i)); + pre = pre.next; + } + return tmp.next; + } + } + public static class Solution2 { + public ListNode swapNodes(ListNode head, int k) { + + // find length of list + int n = 0; + ListNode current = head; + while(current != null){ + current = current.next; + n++; + } + + int nums[] = new int[n]; + current = head; + int i = 0; + while(current != null){ + nums[i++] = current.val; + current = current.next; + } + int firstIndex = 0, secondIndex = 0; + firstIndex = k; + secondIndex = n-k; + int temp = nums[firstIndex-1]; + nums[firstIndex-1] = nums[secondIndex]; + nums[secondIndex] = temp; + ListNode dummy = new ListNode(-1); + current = dummy; + for(i = 0; i Date: Mon, 11 Jan 2021 09:11:21 -0700 Subject: [PATCH 3/4] removed comments and initialized variables on new lines --- src/main/java/com/fishercoder/solutions/_1721.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/fishercoder/solutions/_1721.java b/src/main/java/com/fishercoder/solutions/_1721.java index fb50d4f9a9..82b60289bc 100644 --- a/src/main/java/com/fishercoder/solutions/_1721.java +++ b/src/main/java/com/fishercoder/solutions/_1721.java @@ -51,14 +51,11 @@ public ListNode swapNodes(ListNode head, int k) { nums[i++] = current.val; current = current.next; } - int firstIndex = 0, secondIndex = 0; - - + int firstIndex = 0; + int secondIndex = 0; firstIndex = k; secondIndex = n-k; int temp = nums[firstIndex-1]; - System.out.println("firstIndex: "+firstIndex); - System.out.println("secondIndex: "+secondIndex); nums[firstIndex-1] = nums[secondIndex]; nums[secondIndex] = temp; ListNode dummy = new ListNode(-1); From cdfb3d916236613648ffce725b56f6e970089b65 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Fri, 27 Aug 2021 12:25:58 -0700 Subject: [PATCH 4/4] add solution and testcase for 1721 --- .../java/com/fishercoder/solutions/_1721.java | 35 +++++++++++++++++++ src/test/java/com/fishercoder/_1721Test.java | 12 +++++++ 2 files changed, 47 insertions(+) diff --git a/src/main/java/com/fishercoder/solutions/_1721.java b/src/main/java/com/fishercoder/solutions/_1721.java index 82b60289bc..3bb2f7d83e 100644 --- a/src/main/java/com/fishercoder/solutions/_1721.java +++ b/src/main/java/com/fishercoder/solutions/_1721.java @@ -68,4 +68,39 @@ public ListNode swapNodes(ListNode head, int k) { return dummy.next; } } + public static class Solution3 { + public ListNode swapNodes(ListNode head, int k) { + // O(n) linear time + /* + 1. Calculate length of linked list + 2. Initialize 3 ptrs, temp1 and temp2 used for pointing to nodes at k, (len - k + 1) + and temp3 used to iterate over the linked list + */ + int length = 0; + int secondIndex; + + ListNode temp1 = null, temp2 = null; + ListNode temp3 = head; + while(temp3 != null){ + length++; + temp3 = temp3.next; + } + + secondIndex = length - k + 1; + temp3 = head; + for(int i = 1; i <= length; i++){ + if(i == k){ + temp1 = temp3; + } + if(i == secondIndex){ + temp2 = temp3; + } + temp3 = temp3.next; + } + int value = temp1.val; + temp1.val = temp2.val; + temp2.val = value; + return head; + } + } } diff --git a/src/test/java/com/fishercoder/_1721Test.java b/src/test/java/com/fishercoder/_1721Test.java index ec5dd38a71..ee01b8894f 100644 --- a/src/test/java/com/fishercoder/_1721Test.java +++ b/src/test/java/com/fishercoder/_1721Test.java @@ -1,6 +1,7 @@ package com.fishercoder; import com.fishercoder.common.classes.ListNode; +import com.fishercoder.common.utils.LinkedListUtils; import com.fishercoder.solutions._1721; import org.junit.BeforeClass; import org.junit.Test; @@ -9,13 +10,16 @@ public class _1721Test { private static _1721.Solution2 solution2; + private static _1721.Solution3 solution3; private static ListNode expected; private static ListNode node; private static int k; @BeforeClass public static void setup() { + solution2 = new _1721.Solution2(); + solution3 = new _1721.Solution3(); } @Test @@ -35,4 +39,12 @@ public void test1() { k = 2; assertEquals(expected, solution2.swapNodes(node, k)); } + + @Test + public void test2(){ + node = LinkedListUtils.contructLinkedList(new int[]{90, 100}); + k = 2; + expected = LinkedListUtils.contructLinkedList(new int[]{100, 90}); + assertEquals(expected, solution3.swapNodes(node, k)); + } }