From cb586a8fac4d8f8944b2ba87c843df8b328fde66 Mon Sep 17 00:00:00 2001 From: tk421 <42457203+tk421bsod@users.noreply.github.com> Date: Mon, 11 May 2026 11:49:26 -0700 Subject: [PATCH 1/3] Implement ListNode --- src/ListNode.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..8293484 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -1,3 +1,4 @@ public class ListNode { - + public char data; + public ListNode next; } \ No newline at end of file From e81e5a7c20ca2f80614e0e88f4231cf536d2d286 Mon Sep 17 00:00:00 2001 From: tk421 <42457203+tk421bsod@users.noreply.github.com> Date: Mon, 11 May 2026 11:49:45 -0700 Subject: [PATCH 2/3] Implement printList, countX, main as instructed during class --- src/Practice.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/Practice.java b/src/Practice.java index 34a2f8d..7d2543b 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,54 @@ public class Practice { public static void main(String[] args) { + ListNode myNode = new ListNode(); + myNode.data = 'x'; + ListNode anotherNode = new ListNode(); + anotherNode.data = 't'; + + myNode.next = anotherNode; + + ListNode moreNode = new ListNode(); + moreNode.data = 'r'; + + anotherNode.next = moreNode; + + ListNode coolNode = new ListNode(); + coolNode.data = 'w'; + coolNode.next = myNode; + + ListNode extra = moreNode; + moreNode.data = 'e'; + + ListNode hello = new ListNode(); + hello.data = 'x'; + extra.next = hello; + + printList(coolNode); + System.out.println(countX(coolNode)); + } + + public static void printList(ListNode head) { + ListNode current = head; + + while (current != null) { + System.out.println(current.data); + current = current.next; + } + } + + // Count how many nodes hold an x + public static int countX(ListNode head) { + ListNode current = head; + int xCount = 0; + + while (current != null) { + if (current.data == 'x') { + xCount++; + } + current = current.next; + } + + return xCount; } } From 08af29c802156ba5357c1d62ccbe6d8ed4d6a553 Mon Sep 17 00:00:00 2001 From: tk421 <42457203+tk421bsod@users.noreply.github.com> Date: Wed, 13 May 2026 11:21:23 -0700 Subject: [PATCH 3/3] Implement and use removeAt --- src/Practice.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index 7d2543b..22f540c 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -24,8 +24,11 @@ public static void main(String[] args) { hello.data = 'x'; extra.next = hello; + System.out.println("Original list"); printList(coolNode); - System.out.println(countX(coolNode)); + System.out.println("\nList with 't' removed"); + ListNode listWithAnotherNodeRemoved = removeAt(coolNode, 2); + printList(listWithAnotherNodeRemoved); } public static void printList(ListNode head) { @@ -51,4 +54,24 @@ public static int countX(ListNode head) { return xCount; } + + public static ListNode removeAt(ListNode head, int removeIndex) { + int ind = 0; + ListNode current = head; + ListNode previous = null; + + while (current != null) { + if (ind == removeIndex) { + if (previous == null) { //Removing the first element? Just return the second element as the new head + return head.next; + } + previous.next = current.next; //Point the previous element to the next element, skipping this one + return head; //Head was not modified + } + previous = current; + current = current.next; + ind++; + } + return head; //Head was not modified + } }