Skip to content

Commit f8e1500

Browse files
committed
update cheatsheet
1 parent 293c797 commit f8e1500

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

doc/cheatsheet/2_pointers.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
### 0-2-0) Remove Duplicates from Sorted Array
4545
```java
4646
// java
47-
// LC 26
47+
// LC 26 (LC 83)
4848
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
4949
class Solution {
5050
public int removeDuplicates(int[] nums) {
@@ -68,8 +68,28 @@ class Solution {
6868
}
6969
}
7070
```
71+
### 0-2-1) Remove Element
72+
```java
73+
// java
74+
// LC 27
75+
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
76+
class Solution {
77+
public int removeElement(int[] nums, int val) {
78+
int fast = 0, slow = 0;
79+
while (fast < nums.length) {
80+
if (nums[fast] != val) {
81+
nums[slow] = nums[fast];
82+
slow++;
83+
}
84+
fast++;
85+
}
86+
return slow;
87+
}
88+
}
89+
```
90+
7191

72-
#### 0-2-1) for loop + "expand `left`, `right` from center"
92+
#### 0-2-3) for loop + "expand `left`, `right` from center"
7393
```python
7494
# LC 005 Longest Palindromic Substring
7595
# LC 647 Palindromic Substrings

doc/cheatsheet/2_pointers_linkedlist.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,30 @@ while (fast != null){
143143
return slow;
144144
```
145145

146-
## 2) LC Example
146+
## 2) LC Example
147+
148+
149+
### 2-1) Remove Duplicates from Sorted List
150+
```java
151+
// LC 83 (LC 26)
152+
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
153+
class Solution {
154+
public ListNode deleteDuplicates(ListNode head) {
155+
if (head == null) return null;
156+
ListNode slow = head, fast = head;
157+
while (fast != null) {
158+
if (fast.val != slow.val) {
159+
// nums[slow] = nums[fast];
160+
slow.next = fast;
161+
// slow++;
162+
slow = slow.next;
163+
}
164+
// fast++
165+
fast = fast.next;
166+
}
167+
// 断开与后面重复元素的连接
168+
slow.next = null;
169+
return head;
170+
}
171+
}
172+
```

0 commit comments

Comments
 (0)