From cbda8a98d6a29b2f5b06de792002505c2f2bed14 Mon Sep 17 00:00:00 2001 From: kriseattle Date: Mon, 12 May 2025 11:49:31 -0700 Subject: [PATCH 1/2] Coding along to complete this assignment --- src/ListNode.java | 7 ++++++- src/Practice.java | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..4748bf9 100644 --- a/src/ListNode.java +++ b/src/ListNode.java @@ -1,3 +1,8 @@ public class ListNode { - + public int data; + public ListNode next; + + public ListNode(int data){ + this.data = data; + } } \ No newline at end of file diff --git a/src/Practice.java b/src/Practice.java index 34a2f8d..32cd7d8 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,26 @@ public class Practice { public static void main(String[] args) { - + ListNode head = new ListNode(14); + ListNode nextNumber = new ListNode(7); + + head.next = nextNumber; + nextNumber.next = new ListNode(28); + head.next.next.next = new ListNode(32); + head.next.next.next.next = new ListNode(23); + + // System.out.println(head.data); + // System.out.println(nextNumber.data); // or System.out.println(head.next.data); + + ListNode current = head; + int total = 0; + + System.out.println("Looping:"); + while (current != null){ + //System.out.println(current.data); + total += current.data; + current = current.next; + } + System.out.println(total); + } } From 97082d0e0584c3f2eaca7378d696d3f1514b0eb9 Mon Sep 17 00:00:00 2001 From: kriseattle Date: Wed, 14 May 2025 11:09:04 -0700 Subject: [PATCH 2/2] Coding along to complete this assignment --- src/Practice.java | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 32cd7d8..e6f1f41 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,26 +1,53 @@ public class Practice { public static void main(String[] args) { ListNode head = new ListNode(14); - ListNode nextNumber = new ListNode(7); + ListNode mySeven = new ListNode(7); - head.next = nextNumber; - nextNumber.next = new ListNode(28); + head.next = mySeven; + mySeven.next = new ListNode(28); head.next.next.next = new ListNode(32); head.next.next.next.next = new ListNode(23); // System.out.println(head.data); // System.out.println(nextNumber.data); // or System.out.println(head.next.data); - ListNode current = head; - int total = 0; + // int result = tailData(head); + // System.out.println(result); - System.out.println("Looping:"); + head = removeSecondNode(head); + ListNode current = head; while (current != null){ - //System.out.println(current.data); - total += current.data; + System.out.println(current.data); + current = current.next; + } + + // System.out.println("Looping:"); + // while (current != null){ + // //System.out.println(current.data); + // total += current.data; + // current = current.next; + // } + // System.out.println(total); + + } + + public static int tailData(ListNode head){ + if (head == null){ + return -1; + } + // Current will point to what head is pointing + ListNode current = head; + + while (current.next != null){ current = current.next; } - System.out.println(total); + return current.data; + } + public static ListNode removeSecondNode(ListNode head){ + head.next = head.next.next; + return head; } + + }