Skip to content

Instantly share code, notes, and snippets.

@jordanrios94
Created February 3, 2023 10:22
Show Gist options
  • Save jordanrios94/6d5cf6c62ab546cdcb22814084abd886 to your computer and use it in GitHub Desktop.
Save jordanrios94/6d5cf6c62ab546cdcb22814084abd886 to your computer and use it in GitHub Desktop.
LinkedList From Last
/*
Given a linked list and integer n, return the element n spaces from the last node in the list.
- Do not call the 'size' method of the linked list
- Assume that n will always be less than the length of the list.
*/
function fromLast(list, n) {
let slow = list.getFirst();
let fast = list.getAt(n);
while (fast.next) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment