From ddff5973533e70645281820b027bd34660549f82 Mon Sep 17 00:00:00 2001 From: annaliese_scott <274317330+annaliesescott@users.noreply.github.com> Date: Mon, 11 May 2026 11:48:21 -0700 Subject: [PATCH 1/2] Followed along in class to learn about LinkedLists and how to add and traverse them --- src/ListNode.java | 3 +++ src/Practice.java | 57 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..6aa71e8 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -1,3 +1,6 @@ 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..fbed360 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,60 @@ 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(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){ + //start from head + //print all data in list in order + //keep track of current starting at head + ListNode current = head; + while(current != null){ //until end of list + System.out.println(current.data); //print out data at current + current = current.next; //move current to the next node + } + } + + //count how many nodes hold an x + public static int countX(ListNode head){ + ListNode current = head; + int count = 0; + while(current != null){ + if(current.data == 'x'){ + count++; + } + current = current.next; + } + return count; } } From 4f083d69ea4de5cf0fdd69a276d24ab2715033fc Mon Sep 17 00:00:00 2001 From: annaliese_scott <274317330+annaliesescott@users.noreply.github.com> Date: Wed, 13 May 2026 11:18:34 -0700 Subject: [PATCH 2/2] Followed along in class for day two of linked lists topics --- src/Practice.java | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Practice.java b/src/Practice.java index fbed360..69a5a7d 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -25,7 +25,11 @@ public static void main(String[] args) { hello.data = 'x'; extra.next = hello; - printList(coolNode); + ListNode newHead = removeAt(coolNode, 0); + printList(newHead); + + //int xCount = countX(coolNode); + //System.out.println(xCount); //System.out.println(extra.data); //System.out.println(myNode.next.next.data); @@ -57,4 +61,23 @@ public static int countX(ListNode head){ } return count; } + + //remove the node at removeIndex, and return the head of the list + //example: + // 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