Leetcode#114. Flatten Binary Tree to Linked List
ProblemGiven the root of a binary tree, flatten the tree into a “linked list”:
The “linked list” should use the same TreeNode class where the right child pointer points to the next node in the list and the left child pointer is always null.
The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:
!https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg
123Input: root = [1,2,5,3,4,null,6]Output: [1,null,2,null,3,null,4,null,5,null,6]
Example 2:
1 ...
Leetcode#92. Reverse Linked List II
ProblemGiven the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list.
Example 1:
!https://assets.leetcode.com/uploads/2021/02/19/rev2ex2.jpg
123Input: head = [1,2,3,4,5], left = 2, right = 4Output: [1,4,3,2,5]
Example 2:
123Input: head = [5], left = 1, right = 1Output: [5]
Constraints:
The number of nodes in the list is n.
1 <= n <= 500
500 <= Node.val &l ...
Leetcode#146. LRU Cache
ProblemDesign a data structure that follows the constraints of a Least Recently Used (LRU) cache.
Implement the LRUCache class:
LRUCache(int capacity) Initialize the LRU cache with positive size capacity.
int get(int key) Return the value of the key if the key exists, otherwise return 1.
void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recent ...
Leetcode#117. Populating Next Right Pointers in Each Node II
leetcode116
ProblemGiven a binary tree
1234567struct Node { int val; Node *left; Node *right; Node *next;}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:
1234Input: root = [1,2,3,4,5,null,7]Output: [1,#,2,3,#,4,5,7,#]Explanation:Given the above binary tree (Figure A), your function should populate each next pointer to point to its next righ ...
Leetcode#116. Populating Next Right Pointers in Each Node
ProblemYou are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
1234567struct Node { int val; Node *left; Node *right; Node *next;}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Example 1:
1234Input: root = [1,2,3,4,5,6,7]Output: [1,#,2,3,#,4,5,6,7,#]E ...
Leetcode#23. Merge k Sorted Lists
ProblemYou are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
Example 1:
1234567891011Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,4,4,5,6]Explanation: The linked-lists are:[ 1->4->5, 1->3->4, 2->6]merging them into one sorted list:1->1->2->3->4->4->5->6
Example 2:
123Input: lists = []Output: []
Example 3:
123Input: lists = [[]]Output: [ ...
Leetcode#148. Sort Listk
ProblemGiven the head of a linked list, return the list after sorting it in ascending order.
Example 1:
123Input: head = [4,2,1,3]Output: [1,2,3,4]
Example 2:
123Input: head = [-1,5,3,4,0]Output: [-1,0,3,4,5]
Example 3:
12Input: head = []Output: []
Solve1234567891011121314151617181920212223242526272829303132333435363738394041424344class Solution: def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: # 沒有 或只只有一個 代表已經排好了 if not head or not head.next: ...