PS/LeetCode

LeetCode / Linked List / 19번 / Remove Nth Node From End of List / JS

KimMinJun 2023. 4. 19. 23:33

< 문제 바로가기 >

 

Remove Nth Node From End of List - LeetCode

Can you solve this real interview question? Remove Nth Node From End of List - Given the head of a linked list, remove the nth node from the end of the list and return its head.   Example 1: [https://assets.leetcode.com/uploads/2020/10/03/remove_ex1.jpg]

leetcode.com

 

< 문제 간단설명 >

주어진 Linked List에서 뒤에서 n번째 요소를 제거하고 반환한다.

 

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
  let list1 = head;
  let list2 = head;

  // list1은 list1.length - n개 만큼 남게 된다.
  for (let i = 0; i < n; i += 1) {
    list1 = list1.next;
  }

  // 만약 list1이 끝까지 도달했다면 list1이 n개의 node를 갖고있다는 의미이므로,
  // head를 반환해준다.
  if (!list1) {
    return head.next;
  }

  // list1이 끝까지 도달할 때 까지 반복문을 돌린다.
  // list2는 처음부터 시작하므로 list1.length - n 만큼 next로 가게 된다.
  // 즉 끝에서 거꾸로 n만큼 이동한 효과를 갖는다
  while (list1.next) {
    list1 = list1.next;
    list2 = list2.next;
  }

  // n번째와의 연결을 끊어주고 n+1 번째와 연결해준다.
  list2.next = list2.next.next;

  return head;
};