KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • Level1
  • 제코베 JS 100제
  • 문자열
  • 그래프
  • LeetCode
  • tree
  • js
  • C
  • codeup
  • 백준
  • 다이나믹 프로그래밍
  • Level 0
  • string
  • C++
  • 정렬
  • Level 1
  • 수학
  • programmers
  • Level 2
  • recursion

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun
PS/LeetCode

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

PS/LeetCode

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

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;
};

 

저작자표시 (새창열림)

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS  (0) 2023.04.20
LeetCode / Linked List / 234번 / Palindrome Linked List / JS  (0) 2023.04.19
LeetCode / String / 43번 / Multiply Strings / JS  (0) 2023.04.19
LeetCode / String / 14번 / Longest Common Prefix / JS  (0) 2023.04.19
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS
    • LeetCode / Linked List / 234번 / Palindrome Linked List / JS
    • LeetCode / String / 43번 / Multiply Strings / JS
    • LeetCode / String / 14번 / Longest Common Prefix / JS
    KimMinJun
    KimMinJun

    티스토리툴바

    개인정보

    • 티스토리 홈
    • 포럼
    • 로그인

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.