File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 21
21
| 14| [ 最长公共前缀] ( https://leetcode-cn.com/problems/longest-common-prefix/ ) | [ JavaScript] ( ./algorithms/longest-common-prefix.js ) | Easy|
22
22
| 20| [ 有效的括号] ( https://leetcode.cn/problems/valid-parentheses/ ) | [ JavaScript] ( ./algorithms/valid-parentheses.js ) | Easy|
23
23
| 21| [ 合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists/ ) | [ JavaScript] ( ./algorithms/merge-two-sorted-lists.js ) | Easy|
24
+ | 24| [ 两两交换链表中的节点] ( https://leetcode.cn/problems/swap-nodes-in-pairs/ ) | [ JavaScript] ( ./algorithms/swap-nodes-in-pairs.js ) | Medium|
24
25
| 26| [ 删除有序数组中的重复项] ( https://leetcode.cn/problems/remove-duplicates-from-sorted-array/ ) | [ JavaScript] ( ./algorithms/remove-duplicates-from-sorted-array.js ) | Easy|
25
26
| 27| [ 移除元素] ( https://leetcode.cn/problems/remove-element/ ) | [ JavaScript] ( ./algorithms/remove-element.js ) | Easy|
26
27
| 28| [ 实现 strStr()] ( https://leetcode-cn.com/problems/implement-strstr/ ) | [ JavaScript] ( ./algorithms/implement-strstr.js ) | Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * function ListNode(val, next) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.next = (next===undefined ? null : next)
6
+ * }
7
+ */
8
+ /**
9
+ * @param {ListNode } head
10
+ * @return {ListNode }
11
+ */
12
+ var swapPairs = function ( head ) {
13
+ if ( ! head || ! head . next ) return head ;
14
+ let dummyNode = new ListNode ( - 1 ) ;
15
+ dummyNode . next = head ;
16
+ let prev = dummyNode ;
17
+ let curr = head ;
18
+
19
+ while ( curr && curr . next ) {
20
+ const temp = curr . next . next ;
21
+ prev . next = curr . next ;
22
+ curr . next . next = curr ;
23
+ curr . next = temp ;
24
+ prev = curr ;
25
+ curr = temp ;
26
+ }
27
+
28
+ return dummyNode . next ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments