From e5fb8a613caa070e9dca7125c7465e8ff7596743 Mon Sep 17 00:00:00 2001 From: Mo <274354989+MoTheTurtle@users.noreply.github.com> Date: Mon, 11 May 2026 11:54:35 -0700 Subject: [PATCH 1/4] In class livecode --- src/ListNode.java | 3 ++- src/Practice.java | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 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 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 a64b4e0f45a7f665feccc094c4662ad96ed84af8 Mon Sep 17 00:00:00 2001 From: Mo <274354989+MoTheTurtle@users.noreply.github.com> Date: Wed, 13 May 2026 11:04:59 -0700 Subject: [PATCH 2/4] End of class --- src/Practice.java | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 7d2543b..cf8fc43 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -24,8 +24,12 @@ public static void main(String[] args) { hello.data = 'x'; extra.next = hello; - printList(coolNode); - System.out.println(countX(coolNode)); + //printList(coolNode); + //System.out.println(countX(coolNode)); + + ListNode newHead = removeAt(coolNode, 2); + printList(newHead); + } public static void printList(ListNode head) { @@ -50,5 +54,21 @@ public static int countX(ListNode head) { } return xCount; + + } + +//remove the node at removeIndex, and return the head of the list. +//example: +//e -> t -> k -> y +//removeIndex 2 +// + public static ListNode removeAt(ListNode head, int removeIndex){ + ListNode current = head; + for(int i=0; i < removeIndex - 1 ;i++){ + current = current.next; + } + current.next = current.next.next; + + return head; } } From b9293b86e526341374bff14b778e1c269bb21924 Mon Sep 17 00:00:00 2001 From: Mo <274354989+MoTheTurtle@users.noreply.github.com> Date: Wed, 13 May 2026 11:09:05 -0700 Subject: [PATCH 3/4] my bad, not the end of class --- src/Practice.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index cf8fc43..ed987a0 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -29,7 +29,7 @@ public static void main(String[] args) { ListNode newHead = removeAt(coolNode, 2); printList(newHead); - + } public static void printList(ListNode head) { @@ -59,7 +59,7 @@ public static int countX(ListNode head) { //remove the node at removeIndex, and return the head of the list. //example: -//e -> t -> k -> y +//e -> t -> k -> y -z //removeIndex 2 // public static ListNode removeAt(ListNode head, int removeIndex){ From e7c67421fd1c6a9202d42f66bb4852e2289e00a5 Mon Sep 17 00:00:00 2001 From: Mo <274354989+MoTheTurtle@users.noreply.github.com> Date: Wed, 13 May 2026 11:18:36 -0700 Subject: [PATCH 4/4] end of practice --- src/Practice.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index ed987a0..aa6ef6f 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -27,7 +27,7 @@ public static void main(String[] args) { //printList(coolNode); //System.out.println(countX(coolNode)); - ListNode newHead = removeAt(coolNode, 2); + ListNode newHead = removeAt(coolNode, 0); printList(newHead); } @@ -63,6 +63,9 @@ public static int countX(ListNode head) { //removeIndex 2 // public static ListNode removeAt(ListNode head, int removeIndex){ + if(removeIndex==0){ + return head.next; + } ListNode current = head; for(int i=0; i < removeIndex - 1 ;i++){ current = current.next;