일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Convert Sorted List to Binary Search Tree
- Python Implementation
- iterator
- 프로그래머스
- 715. Range Module
- Regular Expression
- LeetCode
- Decorator
- 30. Substring with Concatenation of All Words
- 시바견
- 43. Multiply Strings
- Class
- attribute
- 파이썬
- Python Code
- DWG
- Protocol
- 컴퓨터의 구조
- shiba
- 밴픽
- 109. Convert Sorted List to Binary Search Tree
- data science
- concurrency
- kaggle
- 315. Count of Smaller Numbers After Self
- Generator
- Substring with Concatenation of All Words
- 운영체제
- t1
- Python
Archives
- Today
- Total
Scribbling
LeetCode: 160. Intersection of Two Linked Lists 본문
Computer Science/Coding Test
LeetCode: 160. Intersection of Two Linked Lists
focalpoint 2021. 12. 2. 20:54O(1) Memory로 어떻게 풀지 고민해보았다.
방법은 두 List의 node 갯수를 파악하는 것이다.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
m, n = 0, 0
cur = headA
while cur != None:
m += 1
cur = cur.next
cur = headB
while cur != None:
n += 1
cur = cur.next
if m >= n:
for t in range(m-n):
headA = headA.next
else:
for t in range(n-m):
headB = headB.next
curA, curB = headA, headB
while curA:
if curA == curB:
return curA
curA = curA.next
curB = curB.next
return None
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 179. Largest Number (0) | 2021.12.04 |
---|---|
LeetCode: 172. Factorial Trailing Zeroes (0) | 2021.12.04 |
LeetCode: 162. Find Peak Element (0) | 2021.12.01 |
LeetCode: 155. Min Stack (0) | 2021.11.30 |
LeetCode: 152. Maximum Product Subarray (0) | 2021.11.29 |