From 7a6f404e63c26bb6d0ce48ca8d0d6d99d1cd8261 Mon Sep 17 00:00:00 2001 From: Un01 <274392693+alexanderpena014-svg@users.noreply.github.com> Date: Mon, 11 May 2026 11:51:21 -0700 Subject: [PATCH 1/2] completed in class tasks --- src/ListNode.java | 3 ++- src/Practice.java | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 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..6853c74 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,72 @@ +import java.util.List; + 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); + + int xCount = countX(coolNode); + System.out.println(xCount); + + //System.out.println(extra.data); + //System.out.println(myNode.next.next.data); + //System.out.println(myNode.data); + //System.out.println(anotherNode.data); + } + + public static void printList(ListNode head) { + // starting from head + // print all data in list in order + + + // keep track of current, starting at head + ListNode current = head; + + // until end of list + while(current != null) { + // print out data at current + System.out.println(current.data); + // move current to the next node + current = current.next; + + } + } + + // count how many nodes hold an x + public static int countX(ListNode head) { + int count = 0; + + ListNode current = head; + while(current != null) { + if(current.data == 'x') { + count++; + } + current = current.next; + } + + return count; } } From 942c47e54bcc4df9a5256b09a8184ab7390155d6 Mon Sep 17 00:00:00 2001 From: Un01 <274392693+alexanderpena014-svg@users.noreply.github.com> Date: Wed, 13 May 2026 11:41:17 -0700 Subject: [PATCH 2/2] inclass work done --- src/Practice.java | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 6853c74..14d7585 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -25,10 +25,12 @@ public static void main(String[] args) { ListNode hello = new ListNode(); hello.data = 'x'; extra.next = hello; - printList(coolNode); + //printList(coolNode); - int xCount = countX(coolNode); - System.out.println(xCount); + ListNode newHead = removeAt(coolNode, 2); + printList(newHead); + //int xCount = countX(coolNode); + //System.out.println(xCount); //System.out.println(extra.data); @@ -69,4 +71,25 @@ public static int countX(ListNode head) { return count; } + + // remove the node at removeIndex, and return the head of the list + // Ex + // e -> t -> k -> y + // removeIndex 2 + // e -> t -> y + + 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; + } + //removes + current.next = current.next.next; + return head; + } }