File tree 4 files changed +151
-0
lines changed
4 files changed +151
-0
lines changed Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - 재귀호출
3
+
4
+ ### Approach
5
+ - 재귀 호출
6
+ - 마지막에 left<>right 이동
7
+
8
+ ### Complexity
9
+ - Time complexity: O(n)
10
+ - Space complexity: O(n)
11
+
12
+
13
+ # Code
14
+
15
+ ``` python
16
+ # Definition for a binary tree node.
17
+ class TreeNode :
18
+ def __init__ (self , val = 0 , left = None , right = None ):
19
+ self .val = val
20
+ self .left = left
21
+ self .right = right
22
+
23
+ class Solution :
24
+ def invertTree (self , root : Optional[TreeNode]) -> Optional[TreeNode]:
25
+ if not root:
26
+ return None
27
+
28
+ left = self .invertTree(root.left)
29
+ right = self .invertTree(root.right)
30
+
31
+ root.left = right
32
+ root.right = left
33
+
34
+ return root
35
+
36
+
37
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+
3
+ ### Approach
4
+ - pos 는 입력값이 아니다.
5
+ - 계속 회전하므로(detour), 언젠가는 one_way 로 가는 값과 만나게 됨.
6
+
7
+ ### Complexity
8
+ - Time complexity: O(n)
9
+ - Space complexity: O(1)
10
+
11
+
12
+ ### Code
13
+
14
+ ``` python
15
+ # Definition for singly-linked list.
16
+ class ListNode :
17
+ def __init__ (self , x ):
18
+ self .val = x
19
+ self .next = None
20
+
21
+ class Solution :
22
+ def hasCycle (self , head : Optional[ListNode]) -> bool :
23
+ if not head or not head.next:
24
+ return False
25
+
26
+ one_way = head
27
+ detour = head.next
28
+
29
+ while detour and detour.next:
30
+ if one_way == detour:
31
+ return True
32
+ one_way = one_way.next
33
+ detour = detour.next.next
34
+
35
+ return False
36
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+
3
+ ### Approach
4
+
5
+ ### Complexity
6
+ - Time complexity: O(n)
7
+ - Space complexity: O(1)
8
+
9
+ ### Code
10
+
11
+ ``` python
12
+ # Definition for singly-linked list.
13
+ class ListNode :
14
+ def __init__ (self , val = 0 , next = None ):
15
+ self .val = val
16
+ self .next = next
17
+
18
+ class Solution :
19
+ def mergeTwoLists (self , l1 : Optional[ListNode], l2 : Optional[ListNode]) -> Optional[ListNode]:
20
+ if not l1:
21
+ return l2
22
+ if not l2:
23
+ return l1
24
+
25
+ merged_list = ListNode()
26
+ current = merged_list
27
+
28
+ while l1 and l2:
29
+ if l1.val <= l2.val:
30
+ current.next = l1
31
+ l1 = l1.next
32
+ else :
33
+ current.next = l2
34
+ l2 = l2.next
35
+
36
+ current = current.next # pointer setting.
37
+
38
+ current.next = l1 if l1 else l2
39
+
40
+ return merged_list.next
41
+ ```
Original file line number Diff line number Diff line change
1
+ ### Intuition
2
+ - 투 포인터 (prev, curr)
3
+
4
+ ### Approach
5
+ - 투 포인터
6
+ - 포인터를 이동하기 전에 prev 가 가리키는 (.next) 를 reverse 해 주어야 한다.
7
+
8
+ ### Complexity
9
+ - Time complexity: O(n)
10
+ - Space complexity: O(1)
11
+
12
+
13
+ ### Code
14
+
15
+ ``` python
16
+ # Definition for singly-linked list.
17
+ class ListNode :
18
+ def __init__ (self , val = 0 , next = None ):
19
+ self .val = val
20
+ self .next = next
21
+
22
+ class Solution :
23
+ def reverseList (self , head : Optional[ListNode]) -> Optional[ListNode]:
24
+ if not head:
25
+ return head
26
+
27
+ prev, curr = None , head
28
+
29
+ while curr:
30
+ next_node = curr.next
31
+ curr.next = prev
32
+ prev = curr
33
+ curr = next_node
34
+
35
+ return prev
36
+
37
+ ```
You can’t perform that action at this time.
0 commit comments