KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • 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 (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Linked List / 148번 / Sort List / JS

2023. 4. 28. 17:19

< 문제 바로가기 >

 

Sort List - LeetCode

Can you solve this real interview question? Sort List - Given the head of a linked list, return the list after sorting it in ascending order.   Example 1: [https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg] Input: head = [4,2,1,3] Output: [1,

leetcode.com

 

< 문제 간단설명 >

리스트에 있는 노드들을 오름차순으로 정렬하는 문제이다.

 

/**
 * 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 sortList = function(head) {
    if(!head) {
        return null;
    }

    let result = [];

    // 모든 노드를 배열에 넣음
    while(head) {
        result.push(head);
        head = head.next;
    }

    // 노드의 value 값으로 오름차순 정렬
    result.sort((a, b) => a.val - b.val);

    // 리스트에 들어있는 노드들을 연결시켜줌
    for(let i=0; i<result.length; i+=1) {
        result[i].next = result[i + 1] || null;
    }

    return result[0];
};
저작자표시

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

LeetCode / Greedy / 621번 / Task Scheduler / JS  (0) 2023.04.28
LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS  (0) 2023.04.28
LeetCode / Linked List / 328번 / Odd Even Linked List / JS  (0) 2023.04.28
LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS  (0) 2023.04.20
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Greedy / 621번 / Task Scheduler / JS
    • LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS
    • LeetCode / Linked List / 328번 / Odd Even Linked List / JS
    • LeetCode / Simulation / 1706번 / Where Will the Ball Fall / JS
    KimMinJun
    KimMinJun

    티스토리툴바