Skip to content

Commit ae52aec

Browse files
committed
add solution for leetcode 024
1 parent 039cf39 commit ae52aec

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ LeetCode
388388
|27|[Remove Element](https://leetcode.com/problems/remove-element/)| [js](./algorithms/removeElement/removeElement.js) [python](./algorithms/removeElement/removeElement.python) |Easy|
389389
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [java](./algorithms/removeDuplicatesFromSortedArray/Solution.java) |Easy|
390390
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| |Hard|
391-
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| |Medium|
391+
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [js](./algorithms/swapNodesInPairs/swapNodesInPairs.js) |Medium|
392392
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)| |Hard|
393393
|22|[Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)| [js](./algorithms/generateParentheses/generateParentheses.js) |Medium|
394394
|21|[Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var swapPairs = function(head) {
2+
let shaob = new ListNode(0); // 添加哨兵 加0
3+
shaob.next = head;
4+
let shao = shaob; //shao 是为了一直往下指, 而 shaob 其实是为了返回第二个节点
5+
while(shao.next != null && shao.next.next != null) {
6+
let start = shao.next; // 代表第一次的1
7+
let end = shao.next.next; // 代表第一次的2
8+
shao.next = end; // 0 指向 2
9+
start.next = end.next; // 1 指向 3
10+
end.next = start; // 2 指向 1
11+
shao = start; // 将此时的1 也就是交换后的1当哨兵 继续循环
12+
}
13+
return shaob.next // 返回的是最初哨兵的前一个 就是链表头
14+
};

0 commit comments

Comments
 (0)