Skip to content

Conversation

github-classroom[bot]
Copy link

@github-classroom github-classroom bot commented Jun 20, 2025

👋! GitHub Classroom created this pull request as a place for your teacher to leave feedback on your work. It will update automatically. Don’t close or merge this pull request, unless you’re instructed to do so by your teacher.
In this pull request, your teacher can leave comments and feedback on your code. Click the Subscribe button to be notified if that happens.
Click the Files changed or Commits tab to see all of the changes pushed to the default branch since the assignment started. Your teacher can see this too.

Notes for teachers

Use this PR to leave feedback. Here are some tips:

  • Click the Files changed tab to see all of the changes pushed to the default branch since the assignment started. To leave comments on specific lines of code, put your cursor over a line of code and click the blue + (plus sign). To learn more about comments, read “Commenting on a pull request”.
  • Click the Commits tab to see the commits pushed to the default branch. Click a commit to see specific changes.
  • If you turned on autograding, then click the Checks tab to see the results.
  • This page is an overview. It shows commits, line comments, and general comments. You can leave a general comment below.
    For more information about this pull request, read “Leaving assignment feedback in GitHub”.

Subscribed: @ryanknj5

for (int i = 0; i < index; i++) { // O(n)
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

LinkedList reversedList = new LinkedList(); // O(1)
LinkedList.Node item = list.head; // O(1)

for (int i = 0; i < list.length(); i++) { // O(n)

Choose a reason for hiding this comment

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

While at first blush, this seemingly looks like an O(n) implementation, what is the time complexity of list.length()? In relation to this for loop's iterations, how often would length() be calculated and what would the resulting complexity be? What do you think about improving our efficiency here for larger list lengths?

Copy link
Collaborator

Choose a reason for hiding this comment

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

changed it so that length is set to variable before

item = item.next; // O(1)
}
return reversedList; // 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


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!

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants