From 0d64861b70c995694e17134f9883e67eb0d07d69 Mon Sep 17 00:00:00 2001 From: fkamau Date: Fri, 13 Oct 2023 20:39:33 -0500 Subject: [PATCH 1/2] Start work on issue #100 From 77410520562a4ad440adaac4019bd2c02dbe9491 Mon Sep 17 00:00:00 2001 From: fkamau Date: Sun, 15 Oct 2023 17:37:03 -0500 Subject: [PATCH 2/2] Reverse nodes in kgroups --- .gitignore | 2 + .../ListNode.java | 72 +++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 Hard/25. Reverse Nodes in k-Group/ListNode.java diff --git a/.gitignore b/.gitignore index 8b76ef3..9fbfaf8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ directories.ipynb +.vscode/ +lib/ diff --git a/Hard/25. Reverse Nodes in k-Group/ListNode.java b/Hard/25. Reverse Nodes in k-Group/ListNode.java new file mode 100644 index 0000000..90027e0 --- /dev/null +++ b/Hard/25. Reverse Nodes in k-Group/ListNode.java @@ -0,0 +1,72 @@ +//Definition for singly-linked list. + +public class ListNode { + int val; + ListNode next; + ListNode() {} + ListNode(int val) { this.val = val; } + ListNode(int val, ListNode next) { this.val = val; this.next = next; } + } + + class Solution{ + public ListNode reverseKGroup(ListNode head, int k){ + //Edge cases when list is null or has less than one node + if(k <= 1 && head == null || head.next == null){ + return head; + } + + //Start by getting the size of the list + ListNode temp = head; + int size = 0; + + while(temp != null){ + size++; + temp = temp.next; + } + + //Initialize curr, temp and newhead pointers to have access points while traversing the list + ListNode curr = head; + ListNode prev = null; + ListNode newHead = null; + + //Run a loop to reverse k group. Only get in this group if size is greater than k + while(size >= k){ + //Initiallize nodes to keep track during reverse process + ListNode last = prev; + ListNode newEnd = curr; + ListNode next = curr.next; + + //loop k times to reverse the k group + for (int i = 0; curr != null && i < k; i++) { + curr.next = prev; + prev = curr; + curr = next; + + //Check to move next if it is not equal to null + if(next != null){ + next = next.next; + } + } + + //Assign last node - node before the the beginning of the next sublist + if(last != null){ + last.next = prev; + }else{ + //Assign the newHead with prev + newHead = prev; + } + + //Point the newEnd to the next node of the main list + newEnd.next = curr; + + //Reduce the size of the list by k times to a void a infinite loop + size -= k; + + //Once the end of a part is done, assign prev with newEnd + prev = newEnd; + } + return newHead; + } + } + + \ No newline at end of file