File tree 2 files changed +32
-0
lines changed 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 25
25
20|[ Valid Parentheses] ( ./0020-valid-parentheses.js ) |Easy|
26
26
21|[ Merge Two Sorted Lists] ( ./0021-merge-two-sorted-lists.js ) |Easy|
27
27
22|[ Generate Parentheses] ( ./0022-generate-parentheses.js ) |Medium|
28
+ 24|[ Swap Nodes in Pairs] ( ./0024-swap-nodes-in-pairs.js ) |Medium|
28
29
27|[ Remove Element] ( ./0027-remove-element.js ) |Easy|
29
30
28|[ Implement strStr()] ( ./0028-implement-strstr.js ) |Easy|
30
31
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 24. Swap Nodes in Pairs
3
+ * https://leetcode.com/problems/swap-nodes-in-pairs/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given a linked list, swap every two adjacent nodes and return its head. You must solve the
7
+ * problem without modifying the values in the list's nodes (i.e., only nodes themselves may
8
+ * be changed.)
9
+ */
10
+
11
+ /**
12
+ * Definition for singly-linked list.
13
+ * function ListNode(val, next) {
14
+ * this.val = (val===undefined ? 0 : val)
15
+ * this.next = (next===undefined ? null : next)
16
+ * }
17
+ */
18
+ /**
19
+ * @param {ListNode } head
20
+ * @return {ListNode }
21
+ */
22
+ var swapPairs = function ( head ) {
23
+ if ( ! head || ! head . next ) return head ;
24
+
25
+ const result = head . next ;
26
+ head . next = result . next ;
27
+ result . next = head ;
28
+ head . next = swapPairs ( head . next ) ;
29
+
30
+ return result ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments