Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Cygra committed Apr 20, 2021
1 parent dcd764f commit be95a5e
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 0024. 两两交换链表中的节点.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 24. 两两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

<https://leetcode-cn.com/problems/swap-nodes-in-pairs/>

---

递归。

```js
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function(head) {
if (!head) return null
if (!head.next) return head
let temp = head.next
head.next = swapPairs(temp.next)
temp.next = head
return temp
};
```
46 changes: 46 additions & 0 deletions 0061. 旋转链表.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 61. 旋转链表

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

<https://leetcode-cn.com/problems/rotate-list/>

---

先让链表屁股指向头(成环),然后用长度减右移的步数,就是左移的步数。移,然后拆断,返回新的头。

注意算术。

```js
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} k
* @return {ListNode}
*/
var rotateRight = function(head, k) {
if (!k) return head
if (!head) return null
if (!head.next) return head
let count = 1
let temp = head
while(temp.next) {
temp = temp.next
count ++
}
temp.next = head
var result = count - k % count - 1
while (result) {
head = head.next
result --
}
var ans = head.next
head.next = null
return ans
};
```
43 changes: 43 additions & 0 deletions 0082. 删除排序链表中的重复元素 II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 82. 删除排序链表中的重复元素 II

存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。

返回同样按升序排列的结果链表。

来源:力扣(LeetCode)

链接:<https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii>

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

---

```js
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
if (!head || !head.next) return head
let dum = new ListNode(0, head)
let current = dum
while (current.next && current.next.next) {
if (current.next.val === current.next.next.val) {
let x = current.next.val
while (current.next && current.next.val === x) {
current.next = current.next.next
}
} else {
current = current.next
}
}
return dum
};
```
68 changes: 68 additions & 0 deletions 0086. 分隔链表.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 86. 分隔链表

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

你应当 保留 两个分区中每个节点的初始相对位置。

来源:力扣(LeetCode)

链接:<https://leetcode-cn.com/problems/partition-list>

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

---

- 先找到最后一个比 x 小的节点,设为 tail
- 再用相邻的两个指针从头遍历链表
- 如果右侧的指针比 x 小,移动到下一个
- 则,右侧的指针比 x 大
- 把右侧指针指向的节点连接到尾节点后面
- 尾结点右移一位

当中,因为要判断第二次遍历的终止条件,所以需要一个额外的指针来代替尾结点来进行右移,原尾结点不动。

```js
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} x
* @return {ListNode}
*/
var partition = function(head, x) {
if (!head || !head.next) return head
let dum = new ListNode(0, head)
let tail = current = head
while (current) {
if (current.val < x) {
tail = current
}
current = current.next
}
let tempTail = tail
let slow = dum, fast = head
while (fast !== tail) {
if (fast.val >= x) {
let temp = fast.next
slow.next = temp
fast.next = tempTail.next
tempTail.next = fast
fast = temp
tempTail = tempTail.next
} else {
slow = fast
fast = fast.next
}
}
return dum.next
};
```

---

其实先找到「第一个比 x 大的」要比先找到「最后一个比 x 小的」要快,而且终止条件的判断也更简单:链表结尾即为遍历终止。
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ Leet Code Solutions Repo
- 19
- 160
- 一个一次走一步,一个一次走两步
- 不要节省变量,能用就用
- 可以添加假的头结点

0 comments on commit be95a5e

Please sign in to comment.