[LeetCode#148] Sort List

2024. 9. 27. 17:31·Algorithm/문제풀이

❒ Description


날짜 2024.09.27 (금)
레벨 Medium
제목 Sort List
링크  https://leetcode.com/problems/sort-list/description/
자료구조  
알고리즘 정렬
시간 복잡도 O(nlogn)

 

 

 

 

 

❒ 문제 분석


시간 복잡도 O(nlogn) 으로 풀어야하는 제약이 있는 문제다. 따라서 병합 정렬을 사용해야 한다.

먼저 병합 정렬의 분할 정복을 위해서는 중앙을 분리해야 한다. 이 문제는 LinkedList를 사용하고 있고,

이 자료 구조는 배열의 길이를 알 수 없다. 따라서 Runner 기법을 사용해야 한다.

 

마지막에는 정복(Conquer)하는 과정이 필요한데, 이 때 오름차순으로 정복해야 한다.

1 c1 = `-1 > 5` c2 = `0 > 3 > 4` c1.val > c2.val (false) return -1 > 0 > 3 > 4 > 5 > null
         
2 c1 = `5 > null` c2 = `0 > 3 > 4` c1.val > c2.val (true) return 0 > 3 > 4 > 5 > null
  c1 =`0 > 3 > 4` c2 = `5 > null`    
         
3 c1 = `3 > 4` c2 = `5 > null` c1.val > c2.val (false) return 3 > 4 > 5 > null
         
4 c1 = `4 > null` c2 = `5 > null` c1.val > c2.val (false) return 4 > 5 > null
         
5 c1 = `null` c2 = `5 > null` c1 == null return 5 > null

 

 

 

 

 

❒ 문제 분석


public ListNode sortList(ListNode head) {
    // 예외 처리
    if (head == null || head.next == null) return head;
    
    // Runner 기법
    ListNode half = null, slow = head, fast = head;
    while (fast != tail) {
        half = slow;
        slow = slow.next;
        fast = fast.next.next;
    }
    
    // 중앙 지점 연결 끊기
    half.next = null;
    
    // Divide
    ListNode chunk1 = sortList(head, slow);
    ListNode chunk2 = sortList(slow.next, tail);
    
    // Conquer
    reteurn mergeSortChunks(chunk1, chunk2);
}

private ListNode mergeSortChunks(ListNode chunk1, ListNode chunk2) {
    if (chunk1 == null) return chunk2;
    if (chunk2 == null) return chunk1;
    
    if (chunk1.val > chunk2.val) {
        ListNode temp  = chunk2;
        chunk2 = chunk1;
        chunk1 = temp;
    }
    chunk1.left = mergeSortChunks(chunk1.next, chunk2);
    return chunk1;
}

 

 

 

 

 

 


'Algorithm > 문제풀이' 카테고리의 다른 글

[LeetCode#147] Insertion Sort List  (0) 2024.09.28
[LeetCode#56] Merge Intervals  (0) 2024.09.27
[LeetCode#105]Construct BT from Preorder and Inorder Traversal  (0) 2024.09.26
[LeetCode#783] Minimum Distance Between BST Nodes  (0) 2024.09.25
[LeetCode#938] Range Sum of BST  (0) 2024.09.25
'Algorithm/문제풀이' 카테고리의 다른 글
  • [LeetCode#147] Insertion Sort List
  • [LeetCode#56] Merge Intervals
  • [LeetCode#105]Construct BT from Preorder and Inorder Traversal
  • [LeetCode#783] Minimum Distance Between BST Nodes
gilbert9172
gilbert9172
gilbert9172 님의 블로그 입니다.
  • gilbert9172
    バックエンド
    gilbert9172
  • 전체
    오늘
    어제
    • All Categories (175)
      • 우테코 7기 (21)
        • 1주차 (8)
        • 2주차 (5)
        • 3주차 (6)
      • Langauge (4)
        • Java (3)
        • Kotlin (1)
      • Back-End (13)
        • SpringBoot (1)
        • Trouble Shooting (0)
        • Setup & Configuration (1)
        • SQL (3)
        • Redis (8)
      • Architecture (6)
        • Multi Module (1)
        • DDD (5)
      • CS (30)
        • Data Structure (6)
        • Operating System (0)
        • Network (12)
        • Database (10)
        • Design Pattern (2)
      • Algorithm (78)
        • 내용 정리 (18)
        • 문제풀이 (60)
      • DevOps (6)
        • AWS (5)
        • Git (1)
      • Front-End (1)
        • Trouble Shooting (1)
      • Project (6)
        • 페이스콕 (6)
      • Book (9)
        • 이벤트 기반 마이크로서비스 구축 (7)
        • 친절한 SQL 튜닝 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    오블완
    Back-Tracking
    부분단조성
    sliding-window
    Two-Pointer
    greedy
    binarysearch
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.2
gilbert9172
[LeetCode#148] Sort List
상단으로

티스토리툴바