Skip to content

Commit b2cce94

Browse files
author
Tushar Borole
committed
24. Swap Nodes in Pairs
1 parent 5ab321a commit b2cce94

File tree

3 files changed

+61
-34
lines changed

3 files changed

+61
-34
lines changed

.idea/workspace.xml

Lines changed: 31 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [Sliding Window Approach](longest_substring.js) | 84 ms | 38.4 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs) |
8787
| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [Iterative](single_number_II.js) | 64 ms | 37.6 MB | Medium | NlogN | | [:link:](https://www.youtube.com/watch?v=KXjgQWDvQ24) |
8888
| 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Ratcheting](container_with_most_water.js) | 52 ms | 35.4 MB | Medium | O(N) | O(1) | [:link:](https://www.youtube.com/watch?v=k5fbSqb9sCI&t=138s) |
89+
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Recursive](swap_nodes_in_pairs.js) | 68 ms | 34 MB | Medium | O(N) | | [:link:](https://www.youtube.com/watch?v=zOovxGmION4) |
8990
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [Sliding Window Approach](k_distinct_characters.js) | 68 ms | 37.1 MB | Hard | NlogN | | [:link:](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1936s) |
9091

9192
## Others

swap_nodes_in_pairs.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// https://leetcode.com/problems/swap-nodes-in-pairs/submissions/
2+
/**
3+
* Definition for singly-linked list.
4+
* function ListNode(val) {
5+
* this.val = val;
6+
* this.next = null;
7+
* }
8+
*/
9+
/**
10+
* @param {ListNode} head
11+
* @return {ListNode}
12+
*/
13+
var swapPairs = function(head) {
14+
if (head === null || head.next === null) {
15+
return head;
16+
}
17+
18+
let node = head.next; //?
19+
head.next = swapPairs(head.next.next); //4, 2
20+
node.next = head;
21+
return node;
22+
};
23+
24+
let node = new ListNode(1);
25+
node.next = new ListNode(2);
26+
node.next.next = new ListNode(3);
27+
node.next.next.next = new ListNode(4);
28+
29+
swapPairs(node); //?

0 commit comments

Comments
 (0)