Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-22041afd0340ce965d47ae6ef1cefeee28c7c493a6346c4f15d667ab976d596c.svg)](https://classroom.github.com/a/a1ZaAUA-)
81 changes: 73 additions & 8 deletions app/src/main/java/assignment/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,85 @@
public class App {

public static int get(LinkedList list, int index) {
return 0;
}
LinkedList.Node item = list.head; // O(1)
int length = list.length();
for (int i = 0; i < length; i++) { // O(n)
if (i == index) {
return item.data;
} // This would make it so that if index is in the bounds of length then it will return the
// data other wise return null right?
item = item.next; // O(1)
}
return item.data; // O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, great implementations of these operations, but consider an edge case we're not actively testing for: what happens if index exceeds the bounds of the linked list?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think i fixed that with the commit

} // Algorithmic Complexity = O(n)

public static void set(LinkedList list, int index, int value) {}
public static void set(LinkedList list, int index, int value) {
LinkedList.Node item = list.head; // O(1)
int length = list.length();
for (int i = 0; i < length; i++) { // O(n)
if (i == index) {
item.data = value;
}
item = item.next; // O(1)
}
} // Algorithmic Complexity = O(n)

public static void remove(LinkedList list, int index) {}
public static void remove(LinkedList list, int index) {

LinkedList.Node item = list.head; // O(1)
if (index == 0) { // O(1)
list.head = list.head.next; // O(1)
}
for (int i = 0; i < index - 1; i++) { // O(n)
item = item.next; // O(1)
}
if (item.next.next == null) { // O(1)
item.next = null; // O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about the item.next.next == null condition; could you explain it to me? Is this intended to serve as a final statement for the case index == 0 and would this strictly be applicable in cases where the list is longer than three elements?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to remove the last thing on a linked list there is no item.next.next so you just set the last thing on the linked list to be null and so it becomes the tail. Otherwise if the index is in the middle area, then i will skip over one link and delete that one. That is my thought process Im not sure if it is right

} else { // O(1)
item.next = item.next.next; // O(1)
}
} // Algorithmic Complexity = O(n)

public static LinkedList reverse(LinkedList list) {
return list;
}
// LinkedList reversedList = new LinkedList(); // O(1)
// LinkedList.Node item = list.head; // O(1)
// int length = list.length();
// for (int i = 0; i < length; i++) { // O(n)
// reversedList.prepend(item.data); // O(1)
// item = item.next; // O(1)
// }
// return reversedList; // O(1)
LinkedList.Node item = list.head; // O(1)
int length = list.length();
int counter = 0;
for (int i = 0; i < length; i++) { // O(n)
list.prepend(item.data); // O(1)
counter++;
remove(list, counter);
item = item.next; // O(1)
}
return list; // O(1)
} // Algorithmic Complexity = O(n)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of prepend()!

Bonus Credit: Curious to get your thoughts on if you see an implementation that allows you to reverse the list in place (i.e. consume no added memory), but perhaps manipulates the original list. The API is not precise enough to indicate what the semantic behaviour should strictly be, but you made the safe assumption given the a list is specified with a return value suggesting that this should not be a destructive operation. Though, as an exercise, do you want to give an in-place implementation a shot just within the Github comments here?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done i think? I commited


public static boolean isSortedAscending(LinkedList list) {
return false;
}
LinkedList.Node item = list.head; // O(1)
while (item.next != null) { // O(n)
if (item.data < item.next.data) { // O(1)
item = item.next; // O(1)
} else { // O(1)
return false; // O(1)
}
}
return true; // O(1)
} // Algorithmic Complexity = O(n)

// Linked List practice
// Write .get
// Write .set
// Write method to delete index
// Reverse linked list
// Is linked list sorted?
// Find algorithmic complexity of each solution

private App() {}
}
16 changes: 16 additions & 0 deletions app/src/test/java/assignment/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ void testRemove() {
assertEquals(3, list.head.next.data);
}

@Test
void testRemoveIndex0() {
var list = new LinkedList();
list.append(1);
list.append(2);
list.append(3);
App.remove(list, 0);

assertEquals(2, list.head.data);
assertEquals(3, list.head.next.data);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 🥇

I appreciate you adding more tests!


@Test
void testReverse() {
var list = new LinkedList();
Expand All @@ -50,6 +62,10 @@ void testReverse() {
list.append(3);
var reversed = App.reverse(list);

System.out.println(reversed.head.data);
System.out.println(reversed.head.next.data);
System.out.println(reversed.head.next.next.data);

assertEquals(3, reversed.head.data);
assertEquals(2, reversed.head.next.data);
assertEquals(1, reversed.head.next.next.data);
Expand Down
Loading