Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
1131153523@qq.com committed Sep 7, 2023
1 parent 86bbcd6 commit 96d1c36
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 51 deletions.
9 changes: 6 additions & 3 deletions code/algorithm/141.环形链表.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
var hasCycle = function (head) {
let fast = head;
let slow = head;
while (fast && fast.next) {
while(fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
if (slow === fast) return true;

if (fast === slow) {
return true
}
}
return false;
return false
};
// @lc code=end
52 changes: 35 additions & 17 deletions code/algorithm/160.相交链表.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,53 @@
var getIntersectionNode = function (headA, headB) {
let cur1 = headA;
let cur2 = headB;
let len1 = 0;
let len2 = 0;
// 获取链表A的长度

// 获取链表长度
let n1 = 0;
while (cur1) {
len1++;
n1++
cur1 = cur1.next;
}
// 获取链表B的长度

// 获取链表长度
let n2 = 0;
while (cur2) {
len2++;
n2++
cur2 = cur2.next;
}
// 恢复头指针

// 恢复指针
cur1 = headA;
cur2 = headB;
// 保证len1永远是最长的链表,len2永远是最短的链表
if (len1 < len2) {
[len1, len2] = [len2, len1][(cur1, cur2)] = [cur2, cur1];

// 交换指针,保证cur1永远是最短的链表
if (n1 > n2) {
[cur1, cur2] = [cur2, cur1]
let temp = n1;
n1 = n2;
n2 = temp;
}
let i = len1 - len2;
// 移动cur1到 倒数len2长度的位置
while (i-- > 0) {
cur1 = cur1.next;

// 先让长的指针移动
let move = n2 - n1;
while(move-- > 0) {
cur2 = cur2.next;
}
// 如果cur1 等于 cur2就停止循环
while (cur1 && cur1 !== cur2) {



// 此时cur1 和 cue2指针保持一致,同时移动两个指针
while(cur1 && cur2) {
if (cur1 === cur2) {
return cur1;
}
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
return null
};
// @lc code=end

// @after-stub-for-debug-begin
module.exports = getIntersectionNode;
// @after-stub-for-debug-end
11 changes: 6 additions & 5 deletions code/algorithm/19.删除链表的倒数第-n-个结点.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ var removeNthFromEnd = function (head, n) {
let ret = new ListNode(-1, head);
let fast = ret;
let slow = ret;
while (n--) {

while(n--) {
fast = fast.next;
}
if (!fast) return ret.next;

while (fast.next) {
while(fast.next) {
fast = fast.next;
slow = slow.next;
}

slow.next = slow.next.next;
return ret.next;

return ret.next
};
// @lc code=end
12 changes: 6 additions & 6 deletions code/algorithm/203.移除链表元素.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
*/
var removeElements = function (head, val) {
let h = new ListNode(-1, head)
let res = h;
while(h.next) {
if (h.next.val === val) {
h.next = h.next.next
let cur = h;
while(cur.next) {
if (cur.next.val === val) {
cur.next = cur.next.next
continue
}
h = h.next
cur = cur.next
}
return res.next;
return h.next
};
// @lc code=end

Expand Down
8 changes: 4 additions & 4 deletions code/algorithm/206.反转链表.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
var reverseList = function (head) {
let pre = null;
let cur = head;
while (cur) {
while(cur) {
const next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
pre = cur
cur = next
}
return pre;
return pre
};
// @lc code=end
36 changes: 20 additions & 16 deletions code/algorithm/24.两两交换链表中的节点.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,30 @@
* @return {ListNode}
*/
var swapPairs = function (head) {
const node = new ListNode(-1, head);
const ret = node;
let temp = node;
// 保证后面有两个节点能进行交换
while (temp.next && temp.next.next) {
let h = new ListNode(-1, head)
let cur = h;

while(cur.next && cur.next.next) {
// 第一个节点
let pre = temp.next;
let first = cur.next;
// 第二个节点
let cur = temp.next.next;
let second = cur.next.next
// 第三个节点
let three = second.next

// 连接实际上的第一个节点
cur.next = second

// 第一个节点 连接 第三个节点
pre.next = cur.next;
// 第二个节点连接 第一个节点
cur.next = pre;
// 开始节点 连接 第二个节点
temp.next = cur;
// 实际的第一个节点 连接 实际第二个节点
second.next = first

// 指针移动
temp = pre;
// 第二个节点 连接 实际第三个节点
first.next = three

// 移动指针到第二个节点
cur = first
}
return ret.next;

return h.next
};
// @lc code=end
2 changes: 2 additions & 0 deletions src/algorithm/链表/24.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 题解

![16940802530111694080252275.png](https://fastly.jsdelivr.net/gh/fyhhub/imgs@main/16940802530111694080252275.png)

```js
/**
* @param {ListNode} head
Expand Down
12 changes: 12 additions & 0 deletions src/informal/每日学习.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ var removeElements = function (head, val) {

2. 算法:[707.设计链表](https://leetcode.cn/problems/design-linked-list/)


## 2023-09-07

1. 算法:翻转链表

2. 算法:交换两个链表的节点

3. 算法:删除链表的倒数第 N 个结点

4. 算法:环形链表

5. 算法:相交链表

0 comments on commit 96d1c36

Please sign in to comment.