Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/main/java/g0101_0200/s0141_linked_list_cycle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
141\. Linked List Cycle

Easy

Given `head`, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to. **Note that `pos` is not passed as a parameter**.

Return `true` _if there is a cycle in the linked list_. Otherwise, return `false`.

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)

**Input:** head = \[3,2,0,-4\], pos = 1

**Output:** true

**Explanation:** There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

**Example 2:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)

**Input:** head = \[1,2\], pos = 0

**Output:** true

**Explanation:** There is a cycle in the linked list, where the tail connects to the 0th node.

**Example 3:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)

**Input:** head = \[1\], pos = -1

**Output:** false

**Explanation:** There is no cycle in the linked list.

**Constraints:**

* The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
* `pos` is `-1` or a **valid index** in the linked-list.

**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
47 changes: 47 additions & 0 deletions src/main/java/g0101_0200/s0142_linked_list_cycle_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
142\. Linked List Cycle II

Medium

Given the `head` of a linked list, return _the node where the cycle begins. If there is no cycle, return_ `null`.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (**0-indexed**). It is `-1` if there is no cycle. **Note that** `pos` **is not passed as a parameter**.

**Do not modify** the linked list.

**Example 1:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)

**Input:** head = \[3,2,0,-4\], pos = 1

**Output:** tail connects to node index 1

**Explanation:** There is a cycle in the linked list, where tail connects to the second node.

**Example 2:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)

**Input:** head = \[1,2\], pos = 0

**Output:** tail connects to node index 0

**Explanation:** There is a cycle in the linked list, where tail connects to the first node.

**Example 3:**

![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)

**Input:** head = \[1\], pos = -1

**Output:** no cycle

**Explanation:** There is no cycle in the linked list.

**Constraints:**

* The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
* `pos` is `-1` or a **valid index** in the linked-list.

**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
34 changes: 34 additions & 0 deletions src/main/java/g0101_0200/s0143_reorder_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
143\. Reorder List

Medium

You are given the head of a singly linked-list. The list can be represented as:

L<sub>0</sub> → L<sub>1</sub> → … → L<sub>n - 1</sub> → L<sub>n</sub>

_Reorder the list to be on the following form:_

L<sub>0</sub> → L<sub>n</sub> → L<sub>1</sub> → L<sub>n - 1</sub> → L<sub>2</sub> → L<sub>n - 2</sub> → …

You may not modify the values in the list's nodes. Only nodes themselves may be changed.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/04/reorder1linked-list.jpg)

**Input:** head = \[1,2,3,4\]

**Output:** \[1,4,2,3\]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/03/09/reorder2-linked-list.jpg)

**Input:** head = \[1,2,3,4,5\]

**Output:** \[1,5,2,4,3\]

**Constraints:**

* The number of nodes in the list is in the range <code>[1, 5 * 10<sup>4</sup>]</code>.
* `1 <= Node.val <= 1000`
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
144\. Binary Tree Preorder Traversal

Easy

Given the `root` of a binary tree, return _the preorder traversal of its nodes' values_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)

**Input:** root = \[1,null,2,3\]

**Output:** \[1,2,3\]

**Example 2:**

**Input:** root = \[\]

**Output:** \[\]

**Example 3:**

**Input:** root = \[1\]

**Output:** \[1\]

**Example 4:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_5.jpg)

**Input:** root = \[1,2\]

**Output:** \[1,2\]

**Example 5:**

![](https://assets.leetcode.com/uploads/2020/09/15/inorder_4.jpg)

**Input:** root = \[1,null,2\]

**Output:** \[1,2\]

**Constraints:**

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`

**Follow up:** Recursive solution is trivial, could you do it iteratively?
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
145\. Binary Tree Postorder Traversal

Easy

Given the `root` of a binary tree, return _the postorder traversal of its nodes' values_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg)

**Input:** root = \[1,null,2,3\]

**Output:** \[3,2,1\]

**Example 2:**

**Input:** root = \[\]

**Output:** \[\]

**Example 3:**

**Input:** root = \[1\]

**Output:** \[1\]

**Example 4:**

![](https://assets.leetcode.com/uploads/2020/08/28/pre3.jpg)

**Input:** root = \[1,2\]

**Output:** \[2,1\]

**Example 5:**

![](https://assets.leetcode.com/uploads/2020/08/28/pre2.jpg)

**Input:** root = \[1,null,2\]

**Output:** \[2,1\]

**Constraints:**

* The number of the nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`

**Follow up:** Recursive solution is trivial, could you do it iteratively?
28 changes: 28 additions & 0 deletions src/main/java/g0101_0200/s0146_lru_cache/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
146\. LRU Cache

Medium

Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**.

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 recently used key.

The functions `get` and `put` must each run in `O(1)` average time complexity.

**Example 1:**

**Input** \["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"\] \[\[2\], \[1, 1\], \[2, 2\], \[1\], \[3, 3\], \[2\], \[4, 4\], \[1\], \[3\], \[4\]\]

**Output:** \[null, null, null, 1, null, -1, null, -1, 3, 4\]

**Explanation:** LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // cache is {1=1} lRUCache.put(2, 2); // cache is {1=1, 2=2} lRUCache.get(1); // return 1 lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} lRUCache.get(2); // returns -1 (not found) lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} lRUCache.get(1); // return -1 (not found) lRUCache.get(3); // return 3 lRUCache.get(4); // return 4

**Constraints:**

* `1 <= capacity <= 3000`
* <code>0 <= key <= 10<sup>4</sup></code>
* <code>0 <= value <= 10<sup>5</sup></code>
* At most 2<code> * 10<sup>5</sup></code> calls will be made to `get` and `put`.
36 changes: 36 additions & 0 deletions src/main/java/g0101_0200/s0147_insertion_sort_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
147\. Insertion Sort List

Medium

Given the `head` of a singly linked list, sort the list using **insertion sort**, and return _the sorted list's head_.

The steps of the **insertion sort** algorithm:

1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
3. It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.

![](https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif)

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/03/04/sort1linked-list.jpg)

**Input:** head = \[4,2,1,3\]

**Output:** \[1,2,3,4\]

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/03/04/sort2linked-list.jpg)

**Input:** head = \[-1,5,3,4,0\]

**Output:** \[-1,0,3,4,5\]

**Constraints:**

* The number of nodes in the list is in the range `[1, 5000]`.
* `-5000 <= Node.val <= 5000`
34 changes: 34 additions & 0 deletions src/main/java/g0101_0200/s0148_sort_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
148\. Sort List

Medium

Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)

**Input:** head = \[4,2,1,3\]

**Output:** \[1,2,3,4\]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)

**Input:** head = \[-1,5,3,4,0\]

**Output:** \[-1,0,3,4,5\]

**Example 3:**

**Input:** head = \[\]

**Output:** \[\]

**Constraints:**

* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>

**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
28 changes: 28 additions & 0 deletions src/main/java/g0101_0200/s0149_max_points_on_a_line/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
149\. Max Points on a Line

Hard

Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)

**Input:** points = \[\[1,1\],\[2,2\],\[3,3\]\]

**Output:** 3

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)

**Input:** points = \[\[1,1\],\[3,2\],\[5,3\],\[4,1\],\[2,3\],\[1,4\]\]

**Output:** 4

**Constraints:**

* `1 <= points.length <= 300`
* `points[i].length == 2`
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
* All the `points` are **unique**.
Loading