Skip to content

Commit c27a427

Browse files
committed
Update readme for tasks 23-49
1 parent 22223a1 commit c27a427

File tree

19 files changed

+122
-960
lines changed

19 files changed

+122
-960
lines changed

src/main/java/g0001_0100/s0023_merge_k_sorted_lists/readme.md

Lines changed: 4 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ _Merge all the linked-lists into one sorted linked-list and return it._
1212

1313
**Output:** [1,1,2,3,4,4,5,6]
1414

15-
**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
15+
**Explanation:** The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6
1616

1717
**Example 2:**
1818

@@ -29,105 +29,8 @@ _Merge all the linked-lists into one sorted linked-list and return it._
2929
**Constraints:**
3030

3131
* `k == lists.length`
32-
* `0 <= k <= 10^4`
32+
* <code>0 <= k <= 10<sup>4</sup></code>
3333
* `0 <= lists[i].length <= 500`
34-
* `-10^4 <= lists[i][j] <= 10^4`
34+
* <code>-10<sup>4</sup> <= lists[i][j] <= 10<sup>4</sup></code>
3535
* `lists[i]` is sorted in **ascending order**.
36-
* The sum of `lists[i].length` won't exceed `10^4`.
37-
38-
To solve the "Merge k Sorted Lists" problem in Java with a `Solution` class, we can use a priority queue (min-heap) to efficiently merge the lists. Here are the steps:
39-
40-
1. Define a `Solution` class.
41-
2. Define a method named `mergeKLists` that takes an array of linked lists `lists` as input and returns a single sorted linked list.
42-
3. Create a priority queue of ListNode objects. We will use this priority queue to store the heads of each linked list.
43-
4. Iterate through each linked list in the input array `lists` and add the head node of each list to the priority queue.
44-
5. Create a dummy ListNode object to serve as the head of the merged sorted linked list.
45-
6. Initialize a ListNode object named `current` to point to the dummy node.
46-
7. While the priority queue is not empty:
47-
- Remove the ListNode with the smallest value from the priority queue.
48-
- Add this node to the merged linked list by setting the `next` pointer of the `current` node to this node.
49-
- Move the `current` pointer to the next node in the merged linked list.
50-
- If the removed node has a `next` pointer, add the next node from the same list to the priority queue.
51-
8. Return the `next` pointer of the dummy node, which points to the head of the merged sorted linked list.
52-
53-
Here's the implementation:
54-
55-
```java
56-
import java.util.PriorityQueue;
57-
58-
public class Solution {
59-
public ListNode mergeKLists(ListNode[] lists) {
60-
PriorityQueue<ListNode> minHeap = new PriorityQueue<>((a, b) -> a.val - b.val);
61-
62-
// Add the heads of all lists to the priority queue
63-
for (ListNode node : lists) {
64-
if (node != null) {
65-
minHeap.offer(node);
66-
}
67-
}
68-
69-
// Create a dummy node to serve as the head of the merged list
70-
ListNode dummy = new ListNode(0);
71-
ListNode current = dummy;
72-
73-
// Merge the lists
74-
while (!minHeap.isEmpty()) {
75-
ListNode minNode = minHeap.poll();
76-
current.next = minNode;
77-
current = current.next;
78-
79-
if (minNode.next != null) {
80-
minHeap.offer(minNode.next);
81-
}
82-
}
83-
84-
return dummy.next;
85-
}
86-
87-
public static void main(String[] args) {
88-
Solution solution = new Solution();
89-
90-
// Test case
91-
ListNode[] lists = new ListNode[] {
92-
ListNode.createList(new int[] {1, 4, 5}),
93-
ListNode.createList(new int[] {1, 3, 4}),
94-
ListNode.createList(new int[] {2, 6})
95-
};
96-
System.out.println("Merged list:");
97-
ListNode.printList(solution.mergeKLists(lists));
98-
}
99-
}
100-
101-
class ListNode {
102-
int val;
103-
ListNode next;
104-
105-
ListNode(int val) {
106-
this.val = val;
107-
}
108-
109-
static ListNode createList(int[] arr) {
110-
if (arr == null || arr.length == 0) {
111-
return null;
112-
}
113-
114-
ListNode dummy = new ListNode(0);
115-
ListNode current = dummy;
116-
for (int num : arr) {
117-
current.next = new ListNode(num);
118-
current = current.next;
119-
}
120-
return dummy.next;
121-
}
122-
123-
static void printList(ListNode head) {
124-
while (head != null) {
125-
System.out.print(head.val + " ");
126-
head = head.next;
127-
}
128-
System.out.println();
129-
}
130-
}
131-
```
132-
133-
This implementation provides a solution to the "Merge k Sorted Lists" problem in Java using a priority queue.
36+
* The sum of `lists[i].length` will not exceed <code>10<sup>4</sup></code>.

src/main/java/g0001_0100/s0024_swap_nodes_in_pairs/readme.md

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,33 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
66

77
**Example 1:**
88

9-
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
10-
119
**Input:** head = [1,2,3,4]
1210

13-
**Output:** [2,1,4,3]
11+
**Output:** [2,1,4,3]
12+
13+
**Explanation:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg)
1416

1517
**Example 2:**
1618

1719
**Input:** head = []
1820

19-
**Output:** []
21+
**Output:** []
2022

2123
**Example 3:**
2224

2325
**Input:** head = [1]
2426

25-
**Output:** [1]
27+
**Output:** [1]
28+
29+
**Example 4:**
30+
31+
**Input:** head = [1,2,3]
32+
33+
**Output:** [2,1,3]
2634

2735
**Constraints:**
2836

2937
* The number of nodes in the list is in the range `[0, 100]`.
30-
* `0 <= Node.val <= 100`
31-
32-
To solve the "Swap Nodes in Pairs" problem in Java with a `Solution` class, we can traverse the linked list while swapping pairs of nodes. Here are the steps:
33-
34-
1. Define a `Solution` class.
35-
2. Define a method named `swapPairs` that takes the head of a linked list as input and returns the head of the modified list.
36-
3. Create a dummy ListNode object and set its `next` pointer to the head of the input list. This dummy node will serve as the new head of the modified list.
37-
4. Initialize three pointers: `prev`, `first`, and `second`.
38-
5. Iterate through the list while `first` and `second` are not null:
39-
- Assign `first` to the `next` pointer of `prev`.
40-
- Assign `second` to the `next` pointer of `first`.
41-
- Assign the `next` pointer of `prev` to the `next` pointer of `second`.
42-
- Assign the `next` pointer of `second` to `first`.
43-
- Move `prev` to `first`.
44-
- Move `first` to `first.next` (which is the next pair of nodes).
45-
6. Return the `next` pointer of the dummy node, which points to the head of the modified list.
46-
47-
Here's the implementation:
48-
49-
```java
50-
public class Solution {
51-
public ListNode swapPairs(ListNode head) {
52-
// Create a dummy node and point its next to the head
53-
ListNode dummy = new ListNode(0);
54-
dummy.next = head;
55-
56-
// Initialize pointers
57-
ListNode prev = dummy;
58-
ListNode first, second;
59-
60-
// Swap pairs of nodes
61-
while (prev.next != null && prev.next.next != null) {
62-
first = prev.next;
63-
second = first.next;
64-
65-
// Swap nodes
66-
prev.next = second;
67-
first.next = second.next;
68-
second.next = first;
69-
70-
// Move prev to the next pair of nodes
71-
prev = first;
72-
}
73-
74-
return dummy.next;
75-
}
76-
}
77-
```
78-
79-
This implementation provides a solution to the "Swap Nodes in Pairs" problem in Java without modifying the values in the list's nodes.
38+
* `0 <= Node.val <= 100`

src/main/java/g0001_0100/s0025_reverse_nodes_in_k_group/readme.md

Lines changed: 5 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Hard
44

5-
Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list.
5+
Given the `head` of a linked list, reverse the nodes of the list `k` at a time, and return _the modified list_.
66

7-
_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes, in the end, should remain as it is.
7+
`k` is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of `k` then left-out nodes, in the end, should remain as it is.
88

99
You may not alter the values in the list's nodes, only nodes themselves may be changed.
1010

@@ -24,89 +24,10 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
2424

2525
**Output:** [3,2,1,4,5]
2626

27-
**Example 3:**
28-
29-
**Input:** head = [1,2,3,4,5], k = 1
30-
31-
**Output:** [1,2,3,4,5]
32-
33-
**Example 4:**
34-
35-
**Input:** head = [1], k = 1
36-
37-
**Output:** [1]
38-
3927
**Constraints:**
4028

41-
* The number of nodes in the list is in the range `sz`.
42-
* `1 <= sz <= 5000`
29+
* The number of nodes in the list is `n`.
30+
* `1 <= k <= n <= 5000`
4331
* `0 <= Node.val <= 1000`
44-
* `1 <= k <= sz`
45-
46-
**Follow-up:** Can you solve the problem in O(1) extra memory space?
47-
48-
To solve the "Reverse Nodes in k-Group" problem in Java with a `Solution` class, we can reverse the nodes in groups of k using a recursive approach. Here are the steps:
49-
50-
1. Define a `Solution` class.
51-
2. Define a method named `reverseKGroup` that takes the head of a linked list and an integer k as input and returns the head of the modified list.
52-
3. Define a helper method named `reverse` that takes the head and tail of a sublist as input and reverses the sublist in place. This method returns the new head of the sublist.
53-
4. Create a dummy ListNode object and set its `next` pointer to the head of the input list. This dummy node will serve as the new head of the modified list.
54-
5. Initialize pointers `prev`, `curr`, `next`, and `tail`. Set `prev` and `tail` to the dummy node, and `curr` to the head of the input list.
55-
6. Iterate through the list:
56-
- Move `curr` k steps forward. If it's not possible (i.e., there are less than k nodes left), break the loop.
57-
- Set `next` to the `next` pointer of `curr`.
58-
- Reverse the sublist from `curr` to `next` using the `reverse` method. Update `prev` and `tail` accordingly.
59-
- Move `prev` and `tail` k steps forward to the last node of the reversed sublist.
60-
- Move `curr` to `next`.
61-
7. Return the `next` pointer of the dummy node, which points to the head of the modified list.
62-
63-
Here's the implementation:
64-
65-
```java
66-
public class Solution {
67-
public ListNode reverseKGroup(ListNode head, int k) {
68-
// Create a dummy node and point its next to the head
69-
ListNode dummy = new ListNode(0);
70-
dummy.next = head;
71-
72-
// Initialize pointers
73-
ListNode prev = dummy, curr = head, next, tail;
74-
75-
// Iterate through the list and reverse in groups of k
76-
while (true) {
77-
// Move curr k steps forward
78-
tail = prev;
79-
for (int i = 0; i < k; i++) {
80-
tail = tail.next;
81-
if (tail == null) return dummy.next; // Less than k nodes left
82-
}
83-
84-
next = tail.next; // Save the next pointer of the sublist
85-
tail.next = null; // Disconnect the sublist from the rest of the list
86-
87-
// Reverse the sublist and update prev and tail pointers
88-
prev.next = reverse(curr, tail);
89-
tail.next = next; // Connect the reversed sublist back to the rest of the list
90-
91-
// Move prev, tail, and curr to the next group
92-
prev = curr;
93-
curr = next;
94-
}
95-
}
96-
97-
// Helper method to reverse a sublist from head to tail
98-
private ListNode reverse(ListNode head, ListNode tail) {
99-
ListNode prev = null, curr = head, next;
100-
while (curr != null) {
101-
next = curr.next;
102-
curr.next = prev;
103-
prev = curr;
104-
curr = next;
105-
if (prev == tail) break;
106-
}
107-
return prev; // Return the new head of the reversed sublist
108-
}
109-
}
110-
```
11132

112-
This implementation provides a solution to the "Reverse Nodes in k-Group" problem in Java without modifying the values in the list's nodes. It recursively reverses the nodes in groups of k.
33+
**Follow-up:** Can you solve the problem in `O(1)` extra memory space?

src/main/java/g0001_0100/s0026_remove_duplicates_from_sorted_array/readme.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@ Easy
44

55
Given an integer array `nums` sorted in **non-decreasing order**, remove the duplicates [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm) such that each unique element appears only **once**. The **relative order** of the elements should be kept the **same**.
66

7-
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
7+
Consider the number of _unique elements_ in `nums` to be `k`. After removing duplicates, return the number of unique elements `k`.
88

9-
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10-
11-
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
9+
The first `k` elements of `nums` should contain the unique numbers in **sorted order**. The remaining elements beyond index `k - 1` can be ignored.
1210

1311
**Custom Judge:**
1412

1513
The judge will test your solution with the following code:
1614

17-
int[] nums = [...]; // Input array
18-
int[] expectedNums = [...]; // The expected answer with correct length
19-
20-
int k = removeDuplicates(nums); // Calls your implementation
21-
22-
assert k == expectedNums.length;
23-
for (int i = 0; i < k; i++) {
24-
assert nums[i] == expectedNums[i];
25-
}
15+
int[] nums = [...]; // Input array int[] expectedNums = [...]; // The expected answer with correct length int k = removeDuplicates(nums); // Calls your implementation assert k == expectedNums.length; for (int i = 0; i < k; i++) { assert nums[i] == expectedNums[i]; }
2616

2717
If all assertions pass, then your solution will be **accepted**.
2818

@@ -44,6 +34,6 @@ If all assertions pass, then your solution will be **accepted**.
4434

4535
**Constraints:**
4636

47-
* <code>0 <= nums.length <= 3 * 10<sup>4</sup></code>
37+
* <code>1 <= nums.length <= 3 * 10<sup>4</sup></code>
4838
* `-100 <= nums[i] <= 100`
4939
* `nums` is sorted in **non-decreasing** order.

src/main/java/g0001_0100/s0027_remove_element/readme.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,18 @@
22

33
Easy
44

5-
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The relative order of the elements may be changed.
5+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm). The order of the elements may be changed. Then return _the number of elements in_ `nums` _which are not equal to_ `val`.
66

7-
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the **first part** of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
7+
Consider the number of elements in `nums` which are not equal to `val` be `k`, to get accepted, you need to do the following things:
88

9-
Return `k` _after placing the final result in the first_ `k` _slots of_ `nums`.
10-
11-
Do **not** allocate extra space for another array. You must do this by **modifying the input array [in-place](https://en.wikipedia.org/wiki/In-place_algorithm)** with O(1) extra memory.
9+
* Change the array `nums` such that the first `k` elements of `nums` contain the elements which are not equal to `val`. The remaining elements of `nums` are not important as well as the size of `nums`.
10+
* Return `k`.
1211

1312
**Custom Judge:**
1413

1514
The judge will test your solution with the following code:
1615

17-
int[] nums = [...]; // Input array
18-
int val = ...; // Value to remove
19-
int[] expectedNums = [...]; // The expected answer with correct length.
20-
// It is sorted with no values equaling val.
21-
22-
int k = removeElement(nums, val); // Calls your implementation
23-
24-
assert k == expectedNums.length;
25-
sort(nums, 0, k); // Sort the first k elements of nums
26-
for (int i = 0; i < actualLength; i++) {
27-
assert nums[i] == expectedNums[i];
28-
}
16+
int[] nums = [...]; // Input array int val = ...; // Value to remove int[] expectedNums = [...]; // The expected answer with correct length. // It is sorted with no values equaling val. int k = removeElement(nums, val); // Calls your implementation assert k == expectedNums.length; sort(nums, 0, k); // Sort the first k elements of nums for (int i = 0; i < actualLength; i++) { assert nums[i] == expectedNums[i]; }
2917

3018
If all assertions pass, then your solution will be **accepted**.
3119

0 commit comments

Comments
 (0)