KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (507)
    • CS (1)
    • Web (29)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • Next.js (5)
      • ETC (1)
    • Docker (14)
    • Git (5)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • PS (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Linked List / 876번 / Middle of Linked List / JS

2023. 4. 1. 00:39

< 문제 바로가기 >

 

Middle of the Linked List - LeetCode

Can you solve this real interview question? Middle of the Linked List - Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node.   Example 1: [https://assets.leetcode.

leetcode.com

 

< 문제 간단설명>

여러개의 노드로 연결된 linked list에서 중간 노드를 반환하면 되는 문제이다. 만약 노드의 개수가 짝수라면 중간 노드의 개수는 2개이므로 그 중 2번째 노드만 반환해준다.

 

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

    while(list2 && list2.next) {
        list1 = list1.next;
        list2 = list2.next.next;
    }

    return list1;
};

배열이라면 쉽게 할 수 있겠지만 Linked List 자료형이므로 인덱스로 접근이 불가하다.

따라서 주어진 head를 복사해서 한칸씩 next로 가는 Linked List 하나랑, 두칸씩 next로 가는 Linked List 하나를 선언해주었다. 따라서 list2가 두칸갈때 list1은 한칸을 가게되는데, 이는 list2가 마지막에 도착하게 된다면 결국 list1은 중간까지만 왔을것이다. 이를 이용해서 list1을 반환해주면 된다.

 

저작자표시 (새창열림)

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

LeetCode / Greedy / 121번 / Best Time to Buy and Sell Stock / JS  (0) 2023.04.01
LeetCode / Linked List / 142번 / Linked List Cycle II / JS  (0) 2023.04.01
LeetCode / Linked List / 206번 / Reverse Linked List / JS  (0) 2023.03.29
LeetCode / Linked List / 21번 / Merge Two Sorted Lists / JS  (0) 2023.03.29
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Greedy / 121번 / Best Time to Buy and Sell Stock / JS
    • LeetCode / Linked List / 142번 / Linked List Cycle II / JS
    • LeetCode / Linked List / 206번 / Reverse Linked List / JS
    • LeetCode / Linked List / 21번 / Merge Two Sorted Lists / JS
    KimMinJun
    KimMinJun

    티스토리툴바