Created
February 3, 2023 10:22
-
-
Save jordanrios94/6d5cf6c62ab546cdcb22814084abd886 to your computer and use it in GitHub Desktop.
LinkedList From Last
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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