Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

/**
Expand All @@ -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);

}

Expand All @@ -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;
}

}

/**
Expand All @@ -82,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;
}
}