-
Notifications
You must be signed in to change notification settings - Fork 0
Feedback #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feedback
Are you sure you want to change the base?
Feedback #1
Changes from all commits
942b865
065e6b2
47d99dd
62d9da8
30260ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[](https://classroom.github.com/a/a1ZaAUA-) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} // 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm curious about the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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