From 4964efd52f00f008fa5e01fb53bf913f2ebdcdae Mon Sep 17 00:00:00 2001 From: Bluenzo <80460870+Bluenzo@users.noreply.github.com> Date: Mon, 2 Jun 2025 14:33:37 -0700 Subject: [PATCH 1/2] 1 left --- src/Exercises.java | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Exercises.java b/src/Exercises.java index 0da86bf..b120ee3 100644 --- a/src/Exercises.java +++ b/src/Exercises.java @@ -12,7 +12,14 @@ public class Exercises { * @return sum of the values in the list */ public static int sum(ListNode head) { - return -1; + int total = 0; + + while(head != null){ + total += head.data; + head = head.next; + } + + return total; } /** @@ -28,7 +35,16 @@ public static int sum(ListNode head) { * @return a count of the negative values in the list */ public static int countNegative(ListNode head) { - return -1; + + int count = 0; + + while(head != null){ + if(head.data < 0){ + count++; + } + head = head.next; + } + return count; } /** @@ -46,6 +62,16 @@ public static int countNegative(ListNode head) { * @param toAdd the value to append in a new node */ public static void addToEnd(ListNode head, int toAdd) { + if (head == null){ + return; + } + + ListNode current = head; + while (current.next != null){ + current = current.next; + } + + current.next = new ListNode(toAdd); } @@ -63,6 +89,18 @@ public static void addToEnd(ListNode head, int toAdd) { */ public static void makePositive(ListNode head) { + if (head == null){ + return; + } + + ListNode current = head; + while(current != null){ + if(current.data < 0){ + current.data = -current.data; + } + current = current.next; + } + } /** From 623b028278b969466a811df58b06b127561153e4 Mon Sep 17 00:00:00 2001 From: Bluenzo <80460870+Bluenzo@users.noreply.github.com> Date: Mon, 2 Jun 2025 23:11:18 -0700 Subject: [PATCH 2/2] Done! --- src/Exercises.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Exercises.java b/src/Exercises.java index b120ee3..bd5a865 100644 --- a/src/Exercises.java +++ b/src/Exercises.java @@ -120,6 +120,19 @@ public static void makePositive(ListNode head) { * @return whether the list is increasing */ public static boolean isIncreasing(ListNode head) { - return false; + + if (head == null || head.next == null){ + return true; + } + + ListNode current = head; + + while(current.next != null){ + if(current.data > current.next.data){ + return false; + } + current = current.next; + } + return true; } }