From b964690cceaa84ef381dfb8feeb8c3ca5d444b2f Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Mon, 12 May 2025 11:49:40 -0700 Subject: [PATCH 1/2] finished live code assignment --- src/ListNode.java | 7 ++++++- src/Practice.java | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ListNode.java b/src/ListNode.java index dd6028c..64488ce 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..9081d96 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -1,5 +1,24 @@ public class Practice { public static void main(String[] args) { + ListNode head = new ListNode(14); + ListNode MySeven = new ListNode(7); + // this sets head.next to MySeven value. + head.next = MySeven; + + // this creates a new Node with a value of 28 which is stored in MySeven.next etc... + MySeven.next = new ListNode(28); + head.next.next = new ListNode(32); + head.next.next.next = new ListNode(23); + + // System.out.println(head); + + ListNode current = head; + int total = 0; + while(current != null) { + total += current.data; + current = current.next; + } + System.out.println(total); } } From b140633ddfca0fff93d4cd1f438dc1f84367bede Mon Sep 17 00:00:00 2001 From: Johncarlo Perez Date: Wed, 14 May 2025 11:12:07 -0700 Subject: [PATCH 2/2] added method --- src/Practice.java | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/Practice.java b/src/Practice.java index 9081d96..12e45ca 100644 --- a/src/Practice.java +++ b/src/Practice.java @@ -11,14 +11,46 @@ public static void main(String[] args) { head.next.next = new ListNode(32); head.next.next.next = new ListNode(23); - // System.out.println(head); + + + // int result = tailData(head); + // System.out.println(result); + + head = removeSecondNode(head); ListNode current = head; - int total = 0; while(current != null) { - total += current.data; + System.out.println(current.data); + current = current.next; + } + + // System.out.println(head); + + // ListNode current = head; + // int total = 0; + // while(current != null) { + // total += current.data; + // current = current.next; + // } + // System.out.println(total); + // } + } + + public static int tailData(ListNode head) { + if(head == null) return -1; + + 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; + } }