Merge pull request #1 from chefyuan/main

first pr
pull/42/head
lin 2021-07-17 16:55:10 +08:00 committed by GitHub
commit 7767ea9330
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 1406 additions and 475 deletions

View File

@ -134,16 +134,19 @@
### 🍅
- [ offer 2 k ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E5%89%91%E6%8C%87offer2%E5%80%92%E6%95%B0%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9.md)
- [ offer 22 k ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E5%89%91%E6%8C%87offer22%E5%80%92%E6%95%B0%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9.md)
- [ 02.03. ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E9%9D%A2%E8%AF%95%E9%A2%98%2002.03.%20%E9%93%BE%E8%A1%A8%E4%B8%AD%E9%97%B4%E8%8A%82%E7%82%B9.md)
- [ offer 52 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E5%89%91%E6%8C%87Offer52%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E8%8A%82%E7%82%B9.md)
- [ offer 52 & leetcode 160 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E5%89%91%E6%8C%87Offer52%E4%B8%A4%E4%B8%AA%E9%93%BE%E8%A1%A8%E7%9A%84%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%85%AC%E5%85%B1%E8%8A%82%E7%82%B9.md)
- [leetcode 234 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/234.%20%E5%9B%9E%E6%96%87%E9%93%BE%E8%A1%A8.md)
- [leetcode 206 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode206%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8.md)
- [leetcode922](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode92%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A82.md)
- [leetcode 92 2](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode92%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A82.md)
- [leetcode 141 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode141%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.md)
- [leetcode 142 2](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode142%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A82.md)
- [leetcode 86 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode86%E5%88%86%E9%9A%94%E9%93%BE%E8%A1%A8.md)
- [leetcode 328 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode328%E5%A5%87%E5%81%B6%E9%93%BE%E8%A1%A8.md)
- [ offer 25 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E5%89%91%E6%8C%87Offer25%E5%90%88%E5%B9%B6%E4%B8%A4%E4%B8%AA%E6%8E%92%E5%BA%8F%E7%9A%84%E9%93%BE%E8%A1%A8.md)
- [leetcode 82 2](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode82%E5%88%A0%E9%99%A4%E6%8E%92%E5%BA%8F%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%87%8D%E5%A4%8D%E5%85%83%E7%B4%A0II.md)
- [leetcode 147 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode147%E5%AF%B9%E9%93%BE%E8%A1%A8%E8%BF%9B%E8%A1%8C%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F.md)
- [ 02.05 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/%E9%9D%A2%E8%AF%95%E9%A2%98%2002.05.%20%E9%93%BE%E8%A1%A8%E6%B1%82%E5%92%8C.md)
### 🚁
@ -214,7 +217,7 @@
### 🌋
-
------
@ -270,4 +273,3 @@
<div align="center"> <img src="https://cdn.jsdelivr.net/gh/tan45du/photobed@master/赞赏码.2mrhxsmxexa0.png" width = "200px" hight = "200px"/> </div>
###### ####

View File

@ -78,6 +78,22 @@ class Solution:
return False
```
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
unordered_map <int, int> m;
for(int i = 0; i < nums.size(); ++i){
if(m.count(nums[i]) && i - m[nums[i]] <= k) return true;
m[nums[i]] = i;
}
return false;
}
};
```
**HashSet**
****
@ -139,4 +155,22 @@ class Solution:
if len(s) > k:
s.remove(nums[i - k])
return False
```
```
C++ Code:
```cpp
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k) {
multiset <int> S;
for(int i = 0; i < nums.size(); ++i){
if(S.count(nums[i])) return true;
S.insert(nums[i]);
if(S.size() > k) S.erase(nums[i - k]);
}
return false;
}
};
```

View File

@ -85,6 +85,42 @@ class Solution {
```
C++ Code:
```cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector <int> arr;
int left = 0, right = matrix[0].size()-1;
int top = 0, down = matrix.size()-1;
while (true) {
for (int i = left; i <= right; ++i) {
arr.emplace_back(matrix[top][i]);
}
top++;
if (top > down) break;
for (int i = top; i <= down; ++i) {
arr.emplace_back(matrix[i][right]);
}
right--;
if (left > right) break;
for (int i = right; i >= left; --i) {
arr.emplace_back(matrix[down][i]);
}
down--;
if (top > down) break;
for (int i = down; i >= top; --i) {
arr.emplace_back(matrix[i][left]);
}
left++;
if (left > right) break;
}
return arr;
}
};
```
Python3 Code:
```python

View File

@ -140,6 +140,42 @@ class Solution:
return arr
```
C++ Code:
```cpp
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector <int> arr;
int left = 0, right = matrix[0].size()-1;
int top = 0, down = matrix.size()-1;
while (true) {
for (int i = left; i <= right; ++i) {
arr.emplace_back(matrix[top][i]);
}
top++;
if (top > down) break;
for (int i = top; i <= down; ++i) {
arr.emplace_back(matrix[i][right]);
}
right--;
if (left > right) break;
for (int i = right; i >= left; --i) {
arr.emplace_back(matrix[down][i]);
}
down--;
if (top > down) break;
for (int i = down; i >= top; --i) {
arr.emplace_back(matrix[i][left]);
}
left++;
if (left > right) break;
}
return arr;
}
};
```
54 ,
Java Code:
@ -226,3 +262,39 @@ class Solution:
return arr.tolist()
```
C++ Code:
```cpp
class Solution {
public:
vector<vector<int>> generateMatrix(int n) {
vector <vector <int>> arr(n, vector <int>(n));
int left = 0, right = n-1, top = 0, buttom = n - 1, num = 1, numsize = n * n;
while (true) {
for (int i = left; i <= right; ++i) {
arr[top][i] = num++;
}
top++;
if (num > numsize) break;
for (int i = top; i <= buttom; ++i) {
arr[i][right] = num++;
}
right--;
if (num > numsize) break;
for (int i = right; i >= left; --i) {
arr[buttom][i] = num++;
}
buttom--;
if (num > numsize) break;
for (int i = buttom; i >= top; --i) {
arr[i][left] = num++;
}
left++;
if (num > numsize) break;
}
return arr;
}
};
```

View File

@ -3,7 +3,6 @@
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
>
#### [66. ](https://leetcode-cn.com/problems/plus-one/)
@ -85,3 +84,22 @@ class Solution:
arr[0] = 1
return arr
```
C++ Code:
```cpp
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
for(int i = digits.size() - 1; i >= 0; --i){
digits[i] = (digits[i] + 1)%10;
if(digits[i]) return digits;
}
for(int & x: digits) x = 0;
digits.emplace_back(1);
reverse(digits.begin(), digits.end());
return digits;
}
};
```

View File

@ -96,6 +96,27 @@ class Solution:
nums[j] = temp
```
C++ Code:
```cpp
class Solution {
public:
void sortColors(vector<int>& nums) {
int len = nums.size(), left = 0;
int i = left, right = len-1;
while (i <= right) {
if (nums[i] == 2) {
swap(nums[i],nums[right--]);
} else if (nums[i] == 0) {
swap(nums[i++],nums[left++]);
} else {
i++;
}
}
}
};
```
使
[0,0,0,1,1,1,2,2,2]
@ -174,4 +195,28 @@ class Solution:
temp = nums[i]
nums[i] = nums[j]
nums[j] = temp
```
```
C++ Code:
```cpp
class Solution {
public:
void sortColors(vector<int>& nums) {
int left = 0, len = nums.size();
int right = len - 1;
for (int i = 0; i <= right; ++i) {
if (nums[i] == 0) {
swap(nums[i],nums[left++]);
}
if (nums[i] == 2) {
swap(nums[i],nums[right--]);
if (nums[i] != 1) {
i--;
}
}
}
}
};
```

View File

@ -1,5 +1,3 @@
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
@ -35,6 +33,8 @@
****
Java Code:
```java
class Solution {
public boolean isPalindrome(ListNode head) {
@ -46,7 +46,7 @@ class Solution {
arr.add(copynode.val);
copynode = copynode.next;
}
// 双指针遍历数组
//双指针遍历数组
int back = 0;
int pro = arr.size() - 1;
while (back < pro) {
@ -61,12 +61,93 @@ class Solution {
return true;
}
}
```
C++ Code:
```cpp
class Solution {
public:
bool isPalindrome(ListNode* head) {
//这里需要用动态数组,因为我们不知道链表的长度
vector<int> arr;
ListNode* copynode = head;
//将链表的值复制到数组中
while (copynode) {
arr.push_back(copynode->val);
copynode = copynode->next;
}
//双指针遍历数组
int back = 0;
int pro = arr.size() - 1;
while (back < pro) {
//判断两个指针的值是否相等
if (arr[back] != arr[pro]) {
return false;
}
//移动指针
back++;
pro--;
}
return true;
}
};
```
JS Code:
```js
var isPalindrome = function(head) {
let arr = [];
let copynode = head;
//将链表的值复制到数组中
while (copynode) {
arr.push(copynode.val)
copynode = copynode.next
}
//双指针遍历数组
let back = 0;
let pro = arr.length - 1;
while (back < pro) {
//判断两个指针的值是否相等
if (arr[back] !== arr[pro]) {
return false
}
//移动指针
back += 1
pro -= 1
}
return true
};
```
Python Code:
```python
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
arr = []
copynode = head
#
while copynode is not None:
arr.append(copynode.val)
copynode = copynode.next
#
back = 0
pro = len(arr) - 1
while back < pro:
#
if arr[back] != arr[pro]:
return False
#
back += 1
pro -= 1
return True
```
****
@ -82,37 +163,35 @@ Java Code
class Solution {
public boolean isPalindrome(ListNode head) {
if (head==null || head.next==null) {
return true;
return true;
}
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
ListNode midenode = searchmidnode(head);
ListNode midnode = searchmidnode(head);
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
//这里我们用的是midnode.next需要注意因为我们找到的是中点但是我们翻转的是后半部分
ListNode backhalf = reverse(midenode.next);
ListNode backhalf = reverse(midnode.next);
//遍历两部分链表,判断值是否相等
ListNode p1 = head;
ListNode p2 = backhalf;
while (p2 != null) {
if (p1.val != p2.val) {
return false;
//若要还原记得这里也要reverse
midnode.next = reverse(backhalf);
return false;
}
p1 = p1.next;
p2 = p2.next;
}
// 还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
//当然如果没有这一步也是可以AC但是面试的时候题目要求可能会有这一条。
midenode.next = reverse(backhalf);
midnode.next = reverse(backhalf);
return true;
}
//找到中间的部分
public ListNode searchmidnode (ListNode head) {
ListNode fast = new ListNode(-1);
ListNode slow = new ListNode(-1);
fast = head;
slow = head;
//找到中点
//找到中点
public ListNode searchmidnode (ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
@ -123,12 +202,11 @@ class Solution {
public ListNode reverse (ListNode slow) {
ListNode low = null;
ListNode temp = null;
//翻转链表
while (slow != null) {
temp = slow.next;
slow.next = low;
low = slow;
slow = temp;
temp = slow.next;
slow.next = low;
low = slow;
slow = temp;
}
return low;
}
@ -144,35 +222,33 @@ public:
if (head == nullptr || head->next == nullptr) {
return true;
}
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
//找到中间节点,也就是翻转的头节点这个在昨天的题目中讲到
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
ListNode * midenode = searchmidnode(head);
ListNode * midnode = searchmidnode(head);
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
//这里我们用的是midnode->next需要注意因为我们找到的是中点但是我们翻转的是后半部分
ListNode * backhalf = reverse(midenode->next);
ListNode * backhalf = reverse(midnode->next);
//遍历两部分链表,判断值是否相等
ListNode * p1 = head;
ListNode * p2 = backhalf;
while (p2 != nullptr) {
if (p1->val != p2->val) {
return false;
//若要还原记得这里也要reverse
midnode->next = reverse(backhalf);
return false;
}
p1 = p1->next;
p2 = p2->next;
}
// 还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
//当然如果没有这一步也是可以AC但是面试的时候题目要求可能会有这一条。
midenode->next = reverse(backhalf);
midnode->next = reverse(backhalf);
return true;
}
//找到中间的部分
ListNode * searchmidnode (ListNode * head) {
ListNode * fast = new ListNode(-1);
ListNode * slow = new ListNode(-1);
fast = head;
slow = head;
//找到中点
ListNode * searchmidnode (ListNode * head) {
ListNode * fast = head;
ListNode * slow = head;
while (fast->next != nullptr && fast->next->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
@ -183,15 +259,119 @@ public:
ListNode * reverse (ListNode * slow) {
ListNode * low = nullptr;
ListNode * temp = nullptr;
//翻转链表
while (slow != nullptr) {
temp = slow->next;
slow->next = low;
low = slow;
slow = temp;
temp = slow->next;
slow->next = low;
low = slow;
slow = temp;
}
return low;
}
};
```
JS Code:
```javascript
var isPalindrome = function(head) {
if (head === null || head.next === null) {
return true;
}
//找到中间节点,也就是翻转的头节点,这个在昨天的题目中讲到
//但是今天和昨天有一些不一样的地方就是,如果有两个中间节点返回第一个,昨天的题目是第二个
let midnode = searchmidnode(head);
//原地翻转链表,需要两个辅助指针。这个也是面试题目,大家可以做一下
//这里我们用的是midnode.next需要注意因为我们找到的是中点但是我们翻转的是后半部分
let backhalf = reverse(midnode.next);
//遍历两部分链表,判断值是否相等
let p1 = head;
let p2 = backhalf;
while (p2 != null) {
if (p1.val != p2.val) {
//若要还原记得这里也要reverse
midnode.next = reverse(backhalf);
return false;
}
p1 = p1.next;
p2 = p2.next;
}
//还原链表并返回结果,这一步是需要注意的,我们不可以破坏初始结构,我们只是判断是否为回文,
//当然如果没有这一步也是可以AC但是面试的时候题目要求可能会有这一条。
midnode.next = reverse(backhalf);
return true;
};
//找到中点
var searchmidnode = function(head) {
let fast = head;
let slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
};
//翻转链表
var reverse = function(slow) {
let low = null;
let temp = null;
while (slow != null) {
temp = slow.next;
slow.next = low;
low = slow;
slow = temp;
}
return low;
};
```
Python Code:
```python
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
if head is None or head.next is None:
return True
#
#
midnode = self.searchmidnode(head)
#
# midnode.next
backhalf = self.reverse(midnode.next)
#
p1 = head
p2 = backhalf
while p2 is not None:
if p1.val != p2.val:
# reverse
midnode.next = self.reverse(backhalf)
return False
p1 = p1.next
p2 = p2.next
#
# AC
midnode.next = self.reverse(backhalf)
return True
#
def searchmidnode(self, head):
fast = head
slow = head
while fast.next is not None and fast.next.next is not None:
fast = fast.next.next
slow = slow.next
return slow
#
def reverse(self, slow):
low = None
temp = None
while slow is not None:
temp = slow.next
slow.next = low
low = slow
slow = temp
return low
```

View File

@ -10,9 +10,9 @@
####
> pos-1
> pos-1
>
> true false
> true false
1
@ -28,7 +28,7 @@
![](https://img-blog.csdnimg.cn/20210321132015849.png)
\
****
@ -42,7 +42,6 @@ Java Code:
```java
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode low = head;
while (fast != null && fast.next != null) {
@ -57,7 +56,28 @@ public class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * fast = head;
ListNode * slow = head;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
return true;
}
}
return false;
}
};
```
JS Code:
```javascript
var hasCycle = function(head) {
let fast = head;
@ -73,23 +93,18 @@ var hasCycle = function(head) {
};
```
C++ Code:
Python Code:
```cpp
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode * fast = head;
ListNode * low = head;
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
low = low->next;
if (fast == low) {
return true;
}
}
return false;
}
};
```python
class Solution:
def hasCycle(self, head: ListNode) -> bool:
fast = head
slow = head
while fast and fast.next:
fast = fast.next.next
slow = slow.next
if fast == slow:
return True
return False
```

View File

@ -18,33 +18,7 @@
```java
public class Solution {
public boolean hasCycle(ListNode head) {
//特殊情况,无节点或只有一个节点的情况
if(head == null || head.next == null){
return false;
}
//设置快慢指针
ListNode pro = head.next;
ListNode last = head;
//循环条件
while( pro != null && pro.next!=null){
pro=pro.next.next;
last=last.next;
//两指针相遇
if(pro == last){
return true;
}
}
//循环结束,指针没有相遇,说明没有环。相当于快指针遍历了一遍链表
return false;
}
}
```
[leetcode 141 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode141%E7%8E%AF%E5%BD%A2%E9%93%BE%E8%A1%A8.md)。
绿
@ -62,23 +36,22 @@ public class Solution {
![image-20201027182649669](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201027182649669.2g8gq4ik6xs0.png)
Java Code:
```java
public class Solution {
public ListNode detectCycle(ListNode head) {
if (head == null) {
return head;
}
if (head.next == null) {
return head.next;
}
//创建新的HashSet,用于保存节点
//创建新的HashSet用于保存节点
HashSet<ListNode> hash = new HashSet<ListNode>();
//遍历链表
while (head != null) {
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
if (hash.contains(head)) {
return head;
}
@ -91,6 +64,74 @@ public class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == nullptr) return head;
if (head->next == nullptr) return head->next;
//创建新的HashSet用于保存节点
set<ListNode *> hash;
//遍历链表
while (head != nullptr) {
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
if (hash.count(head)) {
return head;
}
//不含有,则进行保存,并移动指针
hash.insert(head);
head = head->next;
}
return head;
}
};
```
JS Code:
```javascript
var detectCycle = function(head) {
if (head === null) return head;
if (head.next === null) return head.next;
//创建新的HashSet用于保存节点
let hash = new Set();
//遍历链表
while (head !== null) {
//判断哈希表中是否含有某节点,没有则保存,含有则返回该节点
if (hash.has(head)) {
return head;
}
//不含有,则进行保存,并移动指针
hash.add(head);
head = head.next;
}
return head;
};
```
Python Code:
```python
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
if head is None:
return head
if head.next is None:
return head.next
# HashSet
hash = set()
while head is not None:
#
if head in hash:
return head
#
hash.add(head)
head = head.next
return head
```
###
@ -101,25 +142,25 @@ public class Solution {
**a+(b+c)n+b**
**a+(b+c)n+b**n
b+ca线bna+(b+c)n+ba+(n+1)b+nc
**a+(b+c)m+b**,m
**a+(b+c)m+b**m
2,2
22
**a+(n+1)b+nc=2[a+(m+1)b+mc]****a+b=(n-2m)(b+c)****b+c**
**a+(n+1)b+nc=2[a+(m+1)b+mc]****a+b=(n-2m)(b+c)**
a+bn-2m便n-2m1a+b=b+ca=cheada=c
**b+c**a+bn-2m便n-2m1a+b=b+ca=cheada=c
1.2
1.2
2.
2.
3.head1
3.head1
![2](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/环形链表2.elwu1pw2lw0.gif)
@ -132,25 +173,24 @@ public class Solution {
public ListNode detectCycle(ListNode head) {
//快慢指针
ListNode fast = head;
ListNode low = head;
ListNode slow = head;
//设置循环条件
while (fast != null && fast.next != null) {
fast = fast.next.next;
low = low.next;
slow = slow.next;
//相遇
if (fast == low) {
if (fast == slow) {
//设置一个新的指针从头节点出发慢指针速度为1所以可以使用慢指针从相遇点出发
ListNode newnode = head;
while (newnode != low) {
low = low.next;
newnode = newnode.next;
ListNode newptr = head;
while (newptr != slow) {
slow = slow.next;
newptr = newptr.next;
}
//在环入口相遇
return low;
return slow;
}
}
return null;
}
}
```
@ -163,26 +203,75 @@ public:
ListNode *detectCycle(ListNode *head) {
//快慢指针
ListNode * fast = head;
ListNode * low = head;
ListNode * slow = head;
//设置循环条件
while (fast != nullptr && fast->next != nullptr) {
fast = fast->next->next;
low = low->next;
slow = slow->next;
//相遇
if (fast == low) {
if (fast == slow) {
//设置一个新的指针从头节点出发慢指针速度为1所以可以使用慢指针从相遇点出发
ListNode * newnode = head;
while (newnode != low) {
low = low->next;
while (newnode != slow) {
slow = slow->next;
newnode = newnode->next;
}
//在环入口相遇
return low;
return slow;
}
}
return nullptr;
}
};
```
JS Code:
```js
var detectCycle = function(head) {
//快慢指针
let fast = head;
let slow = head;
//设置循环条件
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
//相遇
if (fast == slow) {
let newptr = head;
//设置一个新的指针从头节点出发慢指针速度为1所以可以使用慢指针从相遇点出发
while (newptr != slow) {
slow = slow.next;
newptr = newptr.next;
}
//在环入口相遇
return slow;
}
}
return null;
};
```
Python Code:
```python
class Solution:
def detectCycle(self, head: ListNode) -> ListNode:
#
fast = head
slow = head
#
while fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
#
if fast is slow:
# 1使
newptr = head
while newptr is not slow:
slow = slow.next
newptr = newptr.next
#
return slow
```

View File

@ -4,11 +4,11 @@
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E5%85%B3%E4%BA%8E%E9%93%BE%E8%A1%A8%E7%9A%84%E9%82%A3%E4%BA%9B%E4%BA%8B.md)
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E7%9B%B4%E6%8E%A5%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F.md)和[【动画模拟】归并排序](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.md)思想的话,可以先复习一下,不然这两道题目会看云里雾里。
[](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E7%9B%B4%E6%8E%A5%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F.md)和[【动画模拟】归并排序](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%E5%92%8C%E7%AE%97%E6%B3%95/%E5%BD%92%E5%B9%B6%E6%8E%92%E5%BA%8F.md)思想的话,可以先复习一下,不然这两道题目会看云里雾里。
#### [147. ](https://leetcode-cn.com/problems/insertion-sort-list/)
@ -30,15 +30,13 @@
On,
O(n)
****
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325113449.75knzw7zmyg0.png)
绿 5
绿 5
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信截图_20210325131349.14mi2ap89uxs.png)
@ -54,13 +52,13 @@
****
****
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/链表的插入排序.4hnc4shp5le0.gif)
@ -68,9 +66,9 @@
3 2 4 243
3 2 4 243
4
4
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/44444444.29mvcvs4yrms.png)
@ -87,7 +85,6 @@ Java Code:
```java
class Solution {
public ListNode insertionSortList(ListNode head) {
if (head == null && head.next == null) {
return head;
}
@ -98,7 +95,6 @@ class Solution {
//判断是否需要执行插入操作
ListNode pre = head.next;
ListNode last = head;
ListNode temphead = dummyNode;
while (pre != null) {
//不需要插入到合适位置,则继续往下移动
if (last.val <= pre.val) {
@ -107,7 +103,7 @@ class Solution {
continue;
}
//开始出发,查找新元素的合适位置
temphead = dummyNode;
ListNode temphead = dummyNode;
while (temphead.next.val <= pre.val) {
temphead = temphead.next;
}
@ -115,10 +111,10 @@ class Solution {
last.next = pre.next;
pre.next = temphead.next;
temphead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
}
}
```
@ -139,7 +135,6 @@ public:
//判断是否需要执行插入操作
ListNode * pre = head->next;
ListNode * last = head;
ListNode * temphead = dummyNode;
while (pre != nullptr) {
//不需要插入到合适位置,则继续往下移动
if (last->val <= pre->val) {
@ -148,7 +143,7 @@ public:
continue;
}
//开始出发,查找新元素的合适位置
temphead = dummyNode;
ListNode * temphead = dummyNode;
while (temphead->next->val <= pre->val) {
temphead = temphead->next;
}
@ -156,6 +151,7 @@ public:
last->next = pre->next;
pre->next = temphead->next;
temphead->next = pre;
//继续往下移动
pre = last->next;
}
return dummyNode->next;
@ -163,5 +159,71 @@ public:
};
```
JS Code:
```javascript
var insertionSortList = function(head) {
if (head === null || head.next === null) return head;
//哑节点
let dummyNode = new ListNode(-1, head);
let pre = head.next;
//pre负责指向新元素last 负责指向新元素的前一元素
//判断是否需要执行插入操作
let last = head;
while (pre) {
//不需要插入到合适位置,则继续往下移动
if (last.val <= pre.val) {
last = last.next;
pre = pre.next;
continue;
}
//开始出发,查找新元素的合适位置
let tempHead = dummyNode;
while (tempHead.next.val <= pre.val) {
tempHead = tempHead.next;
}
//此时我们已经找到了合适位置,我们需要进行插入,大家可以画一画
last.next = pre.next;
pre.next = tempHead.next;
tempHead.next = pre;
//继续往下移动
pre = last.next;
}
return dummyNode.next;
};
```
Python Code:
```python
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
#
dummyNode = ListNode(-1, head)
# prelast
#
pre = head.next
last = head
while pre is not None:
#
if last.val <= pre.val:
pre = pre.next
last = last.next
continue
#
temphead = dummyNode
while temphead.next.val <= pre.val:
temphead = temphead.next
#
last.next = pre.next
pre.next = temphead.next
temphead.next = pre
#
pre = last.next
return dummyNode.next
```

View File

@ -1,6 +1,12 @@
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [206. ](https://leetcode-cn.com/problems/reverse-linked-list/)
@ -19,17 +25,15 @@
low pro head
pro
temp pro
pro pro = pro.next.
pro pro = pro.next
temp low
low = temp
low = temp
@ -41,15 +45,10 @@ Java Code:
```java
class Solution {
public ListNode reverseList(ListNode head) {
//特殊情况
//特殊情况
if (head == null || head.next == null) {
return head;
}
//反转
return reverse(head);
}
public ListNode reverse (ListNode head) {
ListNode low = null;
ListNode pro = head;
while (pro != null) {
@ -64,42 +63,18 @@ class Solution {
}
return low;
}
}
```
JS Code:
```javascript
var reverseList = function(head) {
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
let temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
};
```
C++
C++ Code:
```cpp
class Solution {
public:
ListNode* reverseList(ListNode* head) {
//特殊情况
//特殊情况
if (head == nullptr || head->next == nullptr) {
return head;
}
//反转
return reverse(head);
}
ListNode * reverse (ListNode * head) {
ListNode * low = nullptr;
ListNode * pro = head;
while (pro != nullptr) {
@ -117,6 +92,52 @@ public:
};
```
JS Code:
```javascript
var reverseList = function(head) {
//特殊情况
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
//代表橙色指针
let temp = pro;
//移动绿色指针
pro = pro.next;
//反转节点
temp.next = low;
//移动黄色指针
low = temp;
}
return low;
};
```
Python Code:
```python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#
if head is None or head.next is None:
return head
low = None
pro = head
while pro is not None:
#
temp = pro
# 绿
pro = pro.next
#
temp.next = low
#
low = temp
return low
```
@ -133,8 +154,8 @@ class Solution {
}
//保存最后一个节点
ListNode pro = reverseList(head.next);
//将节点进行反转。我们可以这样理解 4.next.next = 4;
//4.next = 5
//将节点进行反转。我们可以这样理解 4.next.next = 4
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
@ -142,23 +163,9 @@ class Solution {
return pro;
}
}
```
JS Code:
```javascript
var reverseList = function(head) {
if (!head || !head.next) {
return head;
}
let pro = reverseList(head.next);
head.next.next = head;
head.next = null;
return pro;
};
```
C++:
C++ Code:
```cpp
class Solution {
@ -170,8 +177,8 @@ public:
}
//保存最后一个节点
ListNode * pro = reverseList(head->next);
//将节点进行反转。我们可以这样理解 4->next->next = 4;
//4->next = 5
//将节点进行反转。我们可以这样理解 4->next->next = 4
//4->next = 5
//则 5->next = 4 则实现了反转
head->next->next = head;
//防止循环
@ -181,3 +188,61 @@ public:
};
```
JS Code:
```javascript
var reverseList = function(head) {
//结束条件
if (!head || !head.next) {
return head;
}
//保存最后一个节点
let pro = reverseList(head.next);
//将节点进行反转。我们可以这样理解 4.next.next = 4
//4.next = 5
//则 5.next = 4 则实现了反转
head.next.next = head;
//防止循环
head.next = null;
return pro;
};
```
Python Code:
```python
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#
if head is None or head.next is None:
return head
#
pro = self.reverseList(head.next)
# 4->next->next = 4
# 4->next = 5
# 5->next = 4
head.next.next = head
#
head.next = None
return pro
```
<br/>
> [@jaredliw](https://github.com/jaredliw)注:
>
>
>
> ```python
> class Solution:
> def reverseList(self, head: ListNode, prev_nd: ListNode = None) -> ListNode:
> #
> if head is None:
> return prev_nd
> #
> next_nd = head.next
> head.next = prev_nd
> #
> return self.reverseList(next_nd, head)
> ```

View File

@ -26,7 +26,7 @@
####
@ -46,7 +46,7 @@ class Solution {
}
ListNode odd = head;
ListNode even = head.next;
ListNode evenhead = even;
ListNode evenHead = even;
while (odd.next != null && even.next != null) {
//将偶数位合在一起,奇数位合在一起
@ -56,7 +56,7 @@ class Solution {
even = even.next;
}
//链接
odd.next = evenhead;
odd.next = evenHead;
return head;
}
}
@ -73,7 +73,7 @@ public:
}
ListNode * odd = head;
ListNode * even = head->next;
ListNode * evenhead = even;
ListNode * evenHead = even;
while (odd->next != nullptr && even->next != nullptr) {
//将偶数位合在一起,奇数位合在一起
@ -83,7 +83,7 @@ public:
even = even->next;
}
//链接
odd->next = evenhead;
odd->next = evenHead;
return head;
}
};
@ -95,13 +95,36 @@ var oddEvenList = function(head) {
if(!head || !head.next) return head;
let odd = head, even = head.next, evenHead = even;
while(odd.next && even.next){
//将偶数位合在一起,奇数位合在一起
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
//链接
odd.next = evenHead;
return head;
};
```
Python Code:
```python
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
odd = head
even = head.next
evenHead = even
while odd.next is not None and even.next is not None:
#
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
#
odd.next = evenHead
return head
```

View File

@ -6,7 +6,7 @@
#### [82. II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/)
****
@ -35,7 +35,7 @@
![2](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/删除重复节点2.3btmii5cgxa0.gif)
@ -46,20 +46,22 @@ Java Code:
```java
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null||head.next==null){
return head;
}
//侦察兵指针
ListNode pre = head;
ListNode low = new ListNode(0);
low.next = pre;
ListNode ret = new ListNode(-1);
ret = low;
//创建哑节点接上head
ListNode dummy = new ListNode(-1);
dummy.next = head;
//跟随的指针
ListNode low = dummy;
while(pre != null && pre.next != null) {
if (pre.val == pre.next.val) {
//移动侦察兵指针直到找到与上一个不相同的元素
while (pre != null && pre.next != null && pre.val == pre.next.val) {
pre = pre.next;
}
//while循环后pre停留在最后一个重复的节点上
pre = pre.next;
//连上新节点
low.next = pre;
}
else{
@ -67,7 +69,7 @@ class Solution {
low = low.next;
}
}
return ret.next;
return dummy.next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
}
}
```
@ -78,20 +80,22 @@ C++ Code:
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head == nullptr || head->next == nullptr){
return head;
}
//侦察兵指针
ListNode * pre = head;
ListNode * low = new ListNode(0);
low->next = pre;
ListNode * ret = new ListNode(-1);
ret = low;
//创建哑节点接上head
ListNode * dummy = new ListNode(-1);
dummy->next = head;
//跟随的指针
ListNode * low = dummy;
while(pre != nullptr && pre->next != nullptr) {
if (pre->val == pre->next->val) {
//移动侦察兵指针直到找到与上一个不相同的元素
while (pre != nullptr && pre->next != nullptr && pre->val == pre->next->val) {
pre = pre->next;
}
//while循环后pre停留在最后一个重复的节点上
pre = pre->next;
//连上新节点
low->next = pre;
}
else{
@ -99,8 +103,65 @@ public:
low = low->next;
}
}
return ret->next;
return dummy->next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
}
};
```
JS Code:
```javascript
var deleteDuplicates = function(head) {
//侦察兵指针
let pre = head;
//创建虚拟头节点接上head
let dummy = new ListNode(-1);
dummy.next = pre;
//跟随的指针
let low = dummy;
while(pre != null && pre.next != null) {
if (pre.val == pre.next.val) {
//移动侦察兵指针直到找到与上一个不相同的元素
while (pre != null && pre.next != null && pre.val === pre.next.val) {
pre = pre.next;
}
//while循环后pre停留在最后一个重复的节点上
pre = pre.next;
//连上新节点
low.next = pre;
}
else{
pre = pre.next;
low = low.next;
}
}
return dummy.next;//注意这里传回的不是head而是虚拟节点的下一个节点head有可能已经换了
};
```
Python Code:
```python
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
#
pre = head
# head
dummy = ListNode(-1, head)
#
low = dummy
while pre is not None and pre.next is not None:
if pre.val == pre.next.val:
#
while pre is not None and pre.next is not None and pre.val == pre.next.val:
pre = pre.next
# whilepre
pre = pre.next
#
low.next = pre
else:
pre = pre.next
low = low.next
return dummy.next # headhead
```

View File

@ -37,31 +37,20 @@
Java Code:
```java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
if (head == null) {
return head;
}
ListNode pro = head;
ListNode big = new ListNode(-1);
ListNode small = new ListNode(-1);
ListNode headbig = big;
ListNode headsmall =small;
//分
while (pro != null) {
ListNode headsmall = small;
//分
while (pro != null) {
//大于时,放到 big 链表上
if (pro.val >= x) {
big.next = pro;
big = big.next;
// 小于放到 small 链表上
//小于时,放到 small 链表上
}else {
small.next = pro;
small = small.next;
@ -83,21 +72,18 @@ C++ Code:
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (head == nullptr) {
return head;
}
ListNode * pro = head;
ListNode * big = new ListNode(-1);
ListNode * small = new ListNode(-1);
ListNode * headbig = big;
ListNode * headsmall =small;
//分
while (pro != nullptr) {
ListNode * headsmall = small;
//分
while (pro != nullptr) {
//大于时,放到 big 链表上
if (pro->val >= x) {
big->next = pro;
big = big->next;
// 小于放到 small 链表上
//小于时,放到 small 链表上
}else {
small->next = pro;
small = small->next;
@ -113,5 +99,61 @@ public:
};
```
JS Code:
```js
var partition = function(head, x) {
let pro = head;
let big = new ListNode(-1);
let small = new ListNode(-1);
let headbig = big;
let headsmall = small;
//分
while (pro) {
//大于时,放到 big 链表上
if (pro.val >= x) {
big.next = pro;
big = big.next;
//小于时,放到 small 链表上
}else {
small.next = pro;
small = small.next;
}
pro = pro.next;
}
//细节
big.next = null;
//合
small.next = headbig.next;
return headsmall.next;
};
```
Python Code:
```python
class Solution:
def partition(self, head: ListNode, x: int) -> ListNode:
pro = head
big = ListNode(-1)
small = ListNode(-1)
headbig = big
headsmall = small
#
while pro is not None:
# big
if pro.val >= x:
big.next = pro
big = big.next
# small
else:
small.next = pro
small = small.next
pro = pro.next
#
big.next = None
#
small.next = headbig.next
return headsmall.next
```

View File

@ -4,14 +4,12 @@
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
2 1 1 [leetcode 206 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode206%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8.md)
2 1 1 [leetcode 206 ](https://github.com/chefyuan/algorithm-base/blob/main/animation-simulation/%E9%93%BE%E8%A1%A8%E7%AF%87/leetcode206%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8.md)
#### [92. II](https://leetcode-cn.com/problems/reverse-linked-list-ii/)
836
`head` `left` `right` `left <= right` `left` `right` ****
** 1**
@ -43,7 +41,6 @@ Java Code:
```java
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
//虚拟头节点
ListNode temp = new ListNode(-1);
temp.next = head;
@ -53,26 +50,28 @@ class Solution {
for (; i < left-1; ++i) {
pro = pro.next;
}
// 保存 left 节点前的一个节点
//保存 left 节点前的一个节点
ListNode leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro.next;
}
// 保存 right 节点后的节点
//保存 right 节点后的一个节点
ListNode rightNode = pro.next;
//切断链表
pro.next = null;
ListNode newhead = leftNode.next;
leftNode.next = null;
leftNode.next = rever(newhead);
pro.next = null;//切断 right 后的部分
ListNode newhead = leftNode.next;//保存 left 节点
leftNode.next = null;//切断 left 前的部分
//反转
leftNode.next = reverse(newhead);
//重新接头
newhead.next = rightNode;
return temp.next;
}
//和反转链表1代码一致
public ListNode rever (ListNode head) {
ListNode low = null;
public ListNode reverse (ListNode head) {
ListNode low = null;
ListNode pro = head;
while (pro != null) {
ListNode temp = pro;
@ -91,6 +90,7 @@ C++ Code:
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int left, int right) {
//虚拟头节点
ListNode * temp = new ListNode(-1);
temp->next = head;
ListNode * pro = temp;
@ -99,23 +99,26 @@ public:
for (; i < left-1; ++i) {
pro = pro->next;
}
// 保存 left 节点前的一个节点
//保存 left 节点前的一个节点
ListNode * leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro->next;
}
// 保存 right 节点后的节点
//保存 right 节点后的一个节点
ListNode * rightNode = pro->next;
//切断链表
pro->next = nullptr;
ListNode * newhead = leftNode->next;
leftNode->next = nullptr;
leftNode->next = rever(newhead);
pro->next = nullptr;//切断 right 后的部分
ListNode * newhead = leftNode->next;//保存 left 节点
leftNode->next = nullptr;//切断 left 前的部分
//反转
leftNode->next = reverse(newhead);
//重新接头
newhead->next = rightNode;
return temp->next;
}
ListNode * rever (ListNode * head) {
//和反转链表1代码一致
ListNode * reverse (ListNode * head) {
ListNode * low = nullptr;
ListNode * pro = head;
while (pro != nullptr) {
@ -129,3 +132,89 @@ public:
};
```
JS Code:
```js
var reverseBetween = function(head, left, right) {
//虚拟头节点
let temp = new ListNode(-1);
temp.next = head;
let pro = temp;
//来到 left 节点前的一个节点
let i = 0;
for (; i < left-1; ++i) {
pro = pro.next;
}
//保存 left 节点前的一个节点
let leftNode = pro;
//来到 right 节点
for (; i < right; ++i) {
pro = pro.next;
}
//保存 right 节点后的一个节点
let rightNode = pro.next;
//切断链表
pro.next = null;//切断 right 后的部分
let newhead = leftNode.next;//保存 left 节点
leftNode.next = null;//切断 left 前的部分
//反转
leftNode.next = reverse(newhead);
//重新接头
newhead.next = rightNode;
return temp.next;
};
//和反转链表1代码一致
var reverse = function(head) {
let low = null;
let pro = head;
while (pro) {
let temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
};
```
Python Code:
```python
class Solution:
def reverseBetween(self, head: ListNode, left: int, right: int) -> ListNode:
#
temp = ListNode(-1)
temp.next = head
pro = temp
# left
for _ in range(left - 1):
pro = pro.next
# left
leftNode = pro
for _ in range(right - left + 1):
pro = pro.next
# right
rightNode = pro.next
#
pro.next = None # right
newhead = leftNode.next # left
leftNode.next = None # left
#
leftNode.next = self.reverse(newhead)
#
newhead.next = rightNode
return temp.next
# 1
def reverse(self, head):
low = None
pro = head
while pro is not None:
temp = pro
pro = pro.next
temp.next = low
low = temp
return low
```

View File

@ -1,172 +0,0 @@
> **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
>
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [160. ](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
###
ACoffer
![image-20201029215837844](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/image-20201029215837844.7ezoerpghyk0.png)
HashSet
### HashSet
HashsetHashset
****
Java Code
```java
public class Solution {
public ListNode getIntersectionNode (ListNode headA, ListNode headB) {
ListNode tempa = headA;
ListNode tempb = headB;
//定义Hashset
HashSet<ListNode> arr = new HashSet<ListNode>();
while (tempa != null) {
arr.add(tempa);
tempa = tempa.next;
}
while (tempb != null) {
if (arr.contains(tempb)) {
return tempb;
}
tempb = tempb.next;
}
return tempb;
}
}
```
C++ Code:
```cpp
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode * tempa = headA;
ListNode * tempb = headB;
//定义Hashset, cpp对应set
set <ListNode *> arr;
while (tempa != nullptr) {
arr.insert(tempa);
tempa = tempa->next;
}
while (tempb != nullptr) {
if (arr.find(tempb) != arr.end()) {
return tempb;
}
tempb = tempb->next;
}
return tempb;
}
};
```
JS Code:
```javascript
var getIntersectionNode = function(headA, headB) {
let tempa = headA, tempb = headB
const map = new Map()
while(tempa){
map.set(tempa, 1)
tempa = tempa.next
}
while(tempb){
if(map.get(tempb))
return tempb
tempb = tempb.next
}
return tempb
};
```
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/第一次相交的点.5nbxf5t3hgk0.gif)
****
Java Code
```java
public class Solution {
public ListNode getIntersectionNode (ListNode headA, ListNode headB) {
//定义两个节点
ListNode tempa = headA;
ListNode tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != null ? tempa.next:headB;
tempb = tempb != null ? tempb.next:headA;
}
return tempa;
}
}
```
C++ Code
```cpp
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
//定义两个节点
ListNode * tempa = headA;
ListNode * tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != nullptr ? tempa->next: headB;
tempb = tempb != nullptr ? tempb->next: headA;
}
return tempa;
}
};
```
JS Code:
```javascript
var getIntersectionNode = function(headA, headB) {
let tempa = headA, tempb = headB
while(tempa !== tempb){
tempa = tempa ? tempa.next : headB
tempb = tempb ? tempb.next : headA
}
return tempa
};
```

View File

@ -6,7 +6,7 @@
#### [ Offer 25. ](https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof/)
****
@ -15,11 +15,11 @@
1->1->2->3->4->4
```
AC
AC
headpreheadpre.next
headpre headpre.next
@ -80,3 +80,46 @@ public:
};
```
JS Code:
```js
var mergeTwoLists = function(l1, l2) {
let headpro = new ListNode(-1);
let headtemp = headpro;
while (l1 && l2) {
//接上大的那个
if (l1.val >= l2.val) {
headpro.next = l2;
l2 = l2.next;
}
else {
headpro.next = l1;
l1 = l1.next;
}
headpro = headpro.next;
}
headpro.next = l1 != null ? l1:l2;
return headtemp.next;
};
```
Python Code:
```python
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
headpro = ListNode(-1)
headtemp = headpro
while l1 and l2:
#
if l1.val >= l2.val:
headpro.next = l2
l2 = l2.next
else:
headpro.next = l1
l1 = l1.next
headpro = headpro.next
headpro.next = l1 if l1 is not None else l2
return headtemp.next
```

View File

@ -4,13 +4,13 @@
>
> <u>[****](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
#### [ Offer 52. ](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/)
#### [ Offer 52. ](https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/) & [160. 相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/)
###
ACoffer
@ -41,16 +41,19 @@ public class Solution {
ListNode tempb = headB;
//定义Hashset
HashSet<ListNode> arr = new HashSet<ListNode>();
//遍历链表A将所有值都存到arr中
while (tempa != null) {
arr.add(tempa);
tempa = tempa.next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb != null) {
if (arr.contains(tempb)) {
return tempb;
}
tempb = tempb.next;
}
//若上方没有返回此刻tempb为null
return tempb;
}
@ -67,21 +70,73 @@ public:
ListNode * tempb = headB;
//定义Hashset
set <ListNode *> arr;
//遍历链表A将所有值都存到arr中
while (tempa != nullptr) {
arr.insert(tempa);
tempa = tempa->next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb != nullptr) {
if (arr.find(tempb) != arr.end()) {
return tempb;
}
tempb = tempb->next;
}
//若上方没有返回此刻tempb为null
return tempb;
}
};
```
JS Code:
```js
var getIntersectionNode = function(headA, headB) {
let tempa = headA;
let tempb = headB;
//定义Hashset
let arr = new Set();
//遍历链表A将所有值都存到arr中
while (tempa) {
arr.add(tempa);
tempa = tempa.next;
}
//遍历列表B如果发现某个结点已在arr中则直接返回该节点
while (tempb) {
if (arr.has(tempb)) {
return tempb;
}
tempb = tempb.next;
}
//若上方没有返回此刻tempb为null
return tempb;
};
```
Python Code:
```python
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
tempa = headA
tempb = headB
# Hashset
arr = set()
# Aarr
while tempa is not None:
arr.add(tempa)
tempa = tempa.next
# Barr
while tempb is not None:
if tempb in arr:
return tempb
tempb = tempb.next
# tempbnull
return tempb
```
@ -108,7 +163,7 @@ public class Solution {
tempa = tempa != null ? tempa.next: headB;
tempb = tempb != null ? tempb.next: headA;
}
return tempa;
return tempa;//返回tempb也行
}
}
```
@ -124,20 +179,58 @@ public:
ListNode * tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != nullptr ? tempa->next: headB;
tempb = tempb != nullptr ? tempb->next: headA;
}
return tempa;
return tempa;//返回tempb也行
}
};
```
JS Code:
```js
var getIntersectionNode = function(headA, headB) {
//定义两个节点
let tempa = headA;
let tempb = headB;
//循环
while (tempa != tempb) {
//如果不为空就指针下移,为空就跳到另一链表的头部
tempa = tempa != null ? tempa.next: headB;
tempb = tempb != null ? tempb.next: headA;
}
return tempa;//返回tempb也行
};
```
Python Code:
```python
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
#
tempa = headA
tempb = headB
#
while tempa is not tempb:
#
tempa = tempa.next if tempa is not None else headB
tempb = tempb.next if tempb is not None else headA
return tempa # tempb
```
<br/>
> [@jaredliw](https://github.com/jaredliw)注:
>
>
>
> 1. kk
> 2.

View File

@ -12,13 +12,13 @@
n
1038K
1038k
@ -96,15 +96,43 @@ public:
JS Code:
```javascript
var getKthFromEnd = function(head, k) {
//特殊情况
if(!head) return head;
//初始化两个指针, 定义指针指向
let pro = head, after = head;
//先移动绿指针到指定位置
for(let i = 0; i < k - 1; i++){
pro = pro.next;
}
//两个指针同时移动
while(pro.next){
pro = pro.next;
after = after.next;
}
//返回倒数第k个节点
return after;
};
```
```
Python Code:
```python
class Solution:
def getKthFromEnd(self, head: ListNode, k: int) -> ListNode:
#
if head is None:
return head
# ,
pro = head
after = head
# 绿
for _ in range(k - 1):
pro = pro.next
#
while pro.next is not None:
pro = pro.next
after = after.next
# k
return after
```

View File

@ -6,7 +6,7 @@
#### [876. ](https://leetcode-cn.com/problems/middle-of-the-linked-list/)
head
head
@ -17,7 +17,7 @@
3
```
>
>
** 2**
@ -26,9 +26,9 @@
4
```
>
>
##
****
@ -44,7 +44,7 @@
slow
slow
![](https://img-blog.csdnimg.cn/20210321131249789.gif)
@ -71,6 +71,7 @@ class Solution {
C++ Code:
```cpp
class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode * fast = head;//快指针
@ -86,3 +87,34 @@ public:
};
```
JS Code:
```js
var middleNode = function(head) {
let fast = head;//快指针
let slow = head;//慢指针
//循环条件,思考一下跳出循环的情况
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
}
//返回slow指针指向的节点
return slow
};
```
Python Code:
```python
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
fast = head #
slow = head #
#
while fast is not None and fast.next is not None:
fast = fast.next.next
slow = slow.next
# slow
return slow
```

View File

@ -23,22 +23,22 @@
1
```java
(7 -> 1 -> 6) + (5 -> 9 -> 2)617 + 295
2 -> 1 -> 9912
(7 -> 1 -> 6) + (5 -> 9 -> 2) 617 + 295
2 -> 1 -> 9 912
```
2
```java
(9 -> 9) + (9 -> 9),99+99
8->9->1
(9 -> 9) + (9 -> 9) 99 + 99
8 -> 9 -> 1
```
3
```java
(5)+(5),5+5
0->1
(5) + (5) 5 + 5
0 -> 1
```
****
@ -55,11 +55,11 @@
1.
1.
2.
2.
3.1.
3. 1
@ -67,7 +67,7 @@
![](https://cdn.jsdelivr.net/gh/tan45du/photobed@master/photo/链表求和.1yh4ymdee3k0.gif)
nlist1summod1
nlist 1summod 1
@ -78,8 +78,8 @@ Java Code:
```java
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//返回链表
ListNode nList = new ListNode(-1);
//待会儿要返回链表
ListNode nList = new ListNode(-1);//哑节点
ListNode tempnode = nList;
//用来保存进位值初始化为0
int summod = 0;
@ -92,7 +92,7 @@ class Solution {
int sum = l1num+l2num+summod;
//更新进位值例18/10=19/10=0
summod = sum/10;
//新节点保存的值18 % 8=2则添加2
//新节点保存的值18%8=2则添加2
sum = sum%10;
//添加节点
tempnode.next = new ListNode(sum);
@ -106,10 +106,10 @@ class Solution {
}
}
//最后根据进位值判断需不需要继续添加节点
if (summod == 1) {
if (summod != 0) {
tempnode.next = new ListNode(summod);
}
return nList.next;
return nList.next;//去除哑节点
}
}
```
@ -120,8 +120,8 @@ C++ Code:
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
//返回链表
ListNode * nList = new ListNode(-1);
//待会儿要返回链表
ListNode * nList = new ListNode(-1);//哑节点
ListNode * tempnode = nList;
//用来保存进位值初始化为0
int summod = 0;
@ -134,7 +134,7 @@ public:
int sum = l1num + l2num + summod;
//更新进位值例18/10=19/10=0
summod = sum / 10;
//新节点保存的值18 % 8=2则添加2
//新节点保存的值18%8=2则添加2
sum = sum % 10;
//添加节点
tempnode->next = new ListNode(sum);
@ -148,11 +148,85 @@ public:
}
}
//最后根据进位值判断需不需要继续添加节点
if (summod == 1) {
if (summod != 0) {
tempnode->next = new ListNode(summod);
}
return nList->next;
return nList->next;//哑节点
}
};
```
JS Code:
```js
var addTwoNumbers = function(l1, l2) {
//待会儿要返回的链表
let nList = new ListNode(-1);//哑节点
let tempnode = nList;
//用来保存进位值初始化为0
let summod = 0;
while (l1 || l2) {
//如果l1的链表为空则l1num为0若是不为空则为链表的节点值
//判断是否为空为空就设为0
let l1num = l1 === null ? 0 : l1.val;
let l2num = l2 === null ? 0 : l2.val;
//将链表的值和进位值相加,得到为返回链表的值
let sum = l1num + l2num + summod;
//更新进位值例18/10=19/10=0
summod = ~~(sum / 10);
//新节点保存的值18%8=2则添加2
sum = sum % 10;
//添加节点
tempnode.next = new ListNode(sum);
//移动指针
tempnode = tempnode.next;
if (l1) {
l1 = l1.next;
}
if (l2) {
l2 = l2.next;
}
}
//最后根据进位值判断需不需要继续添加节点
if (summod !== 0) {
tempnode.next = new ListNode(summod);
}
return nList.next;//去除哑节点
};
```
Python Code:
```python
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
#
nList = ListNode(-1) #
tempnode = nList
# 0
summod = 0
while l1 is not None o l2 is not None:
# l1l1num0
# 0
l1num = 0 if l1 is None else l1.val
l2num = 0 if l2 is None else l2.val
#
sum_ = l1num + l2num + summod
# 18/10=19/10=0
# 1 %8=22
# 使divmod
summod, sum_ = divmod(sum_, 10)
#
tempnode.next = ListNode(sum_)
#
tempnode = tempnode.next
if l1 is not None:
l1 = l1.next
if l2 is not None:
l2 = l2.next
#
if summod != 0:
tempnode.next = ListNode(summod)
return nList.next #
```