Notice
														
												
											
												
												
													Recent Posts
													
											
												
												
													Recent Comments
													
											
												
												
													Link
													
											
									| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
													Tags
													
											
												
												- MainScheduler
 - 코테
 - swift
 - GCD
 - RaceCondition
 - cleanarchitecture
 - MainScheduler.Instance
 - SeSAC
 - DynamicMemberLookup
 - SwiftUI
 - DependencyInjection
 - 등굣길
 - 프로그래머스
 - baseviewcontroller
 - combine
 - MethodSwilzzling
 - 명품cppProgramming c++
 - data_structure
 - DiffableDataSource
 - 청년취업사관학교
 - SRP
 - CoreBluetooth
 - rxswift
 - 오픈채팅방
 - gitflow
 - leetcode
 - IOS
 - DispatchQueue
 - Realm
 - MainScheduler.asyncInstance
 
													Archives
													
											
												
												- Today
 
- Total
 
Do.
LeetCode - Merge Two Sorted Lists, Swift 본문
https://leetcode.com/problems/merge-two-sorted-lists/
Merge Two Sorted Lists - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
오랜만에 리트코드
- 두개의 `정렬된` 링크드 리스트를 하나의 정렬된 리스트로 만들어 내는 것
- 기본 알고리즘은 아래와 같다.

위와 같이 두개의 링크드 리스트가 있으면 각 리스트를 노드를 가리키는 참조 인스턴스가 있다.
Left와 Right의 CurrentNode의 Val중 누가 더 작은지 대소 비교를 해서 더 작은 노드를 Head의 next Node로 전달한다.

지금은 Left의 CurrentNode는 1이고, Right의 CurrentNode 3이므로 또 3과 1을 대소 비교 한다.

이 작업을 다음 노드가 없을 때 까지 반복 해주면 된다. 아주 간단!
코드로는 아래와 같다.
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public var val: Int
 *     public var next: ListNode?
 *     public init() { self.val = 0; self.next = nil; }
 *     public init(_ val: Int) { self.val = val; self.next = nil; }
 *     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
 * }
 */
class Solution {
    func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? {
        
        var leftNode = list1
        var rightNode = list2
        var head: ListNode? = ListNode(101)
        var currentNode: ListNode? = head
        while let node = currentNode {
            if leftNode != nil, rightNode != nil {
                if leftNode!.val < rightNode!.val {
                    node.next = leftNode
                    leftNode = leftNode?.next
                } else {
                    node.next = rightNode
                    rightNode = rightNode?.next
                }
            } else {
                node.next = leftNode != nil ? leftNode : rightNode
                break
            }
            currentNode = node.next
        }
        return head?.next
    }
}
'Algorithm' 카테고리의 다른 글
| BOJ1158 - 요세푸스 문제, Swift (0) | 2022.03.25 | 
|---|---|
| 프로그래머스 - 등굣길, Cpp (0) | 2022.03.22 | 
| 2022 KAKAO BLIND RECRUITMENT - 양궁대회 (0) | 2022.03.18 | 
| 2022 KAKAO BLIND RECRUITMENT - k진수에서 소수 개수 구하기, Swift (0) | 2022.03.15 | 
| 2022 KAKAO BLIND RECRUITMENT - 주차요금 계산하기, Swift (0) | 2022.03.14 | 
			  Comments