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 | 31 |
Tags
- 사당돈
- algorithum
- two pointers
- tree
- map
- 코테
- two sum
- 6월 첼린지
- 알탕뚝배기
- 해물감성포차
- Array
- Python
- 발리인망원
- LeetCode
- linked list
- 코딩 테스트
- 투포인터
- invert Binary Tree
- Find the Duplicate Number
- 망원 밥집
- 알고리즘
- 른당사피
- string
- java
- findDulicate
- algorithm
- 코딩 테세트
- 발리 음식
- prefix sum
- 맛집
Archives
- Today
- Total
기록하는 공간
[leetcode/python3] 21. Merge Two Sorted Lists 본문
Merge Two Sorted Lists - LeetCode
Merge Two Sorted Lists - You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list.
leetcode.com
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy = ListNode()
head = dummy
while list1 != None and list2 != None:
if list1.val < list2.val:
head.next = list1
list1 = list1.next
else:
head.next = list2
list2 = list2.next
head = head.next
head.next = list1 or list2
return dummy.next
자료구조는 Linked List를 사용
'알고리즘 > leetcode' 카테고리의 다른 글
[leetcode/python3] 876. Middle of the Linked List (0) | 2023.02.10 |
---|---|
[leetcode/python3] 206. Reverse Linked List (0) | 2023.02.09 |
[leetcode/python3] 392. Is Subsequence (1) | 2023.01.25 |
[leetcode/python3] 205. Isomorphic Strings (0) | 2023.01.25 |
[leetcode/python3] 724. Find Pivot Index (0) | 2023.01.22 |
Comments