[JavaScript] formlast-linked list

bbio3o·2021년 2월 1일
0
post-thumbnail

Task

--- Directions
Given a linked list, 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.
--- Examples
const list = new List();
list.insertLast('a');
list.insertLast('b');
list.insertLast('c');
list.insertLast('d');
fromLast(list, 2).data // 'b'

Solution

function fromLast(list, n) {
  let slow = list.getFirst();
  let fast = list.getFirst();

  while (n > 0) {
    fast = fast.next;
    n--;
  }

  while (fast.next) {
    slow = slow.next;
    fast = fast.next;
  }

  return slow;
}
profile
그림도 그리는 개발자 🎨👩‍💻

0개의 댓글