Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 075a57f

Browse files
update: 24
1 parent fa07a98 commit 075a57f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
2424
| 17 | [Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/) | [JavaScript](./src/letter-combinations-of-a-phone-number/res.js) | Medium |
2525
| 19 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/) | [JavaScript](./src/remove-nth-node-from-end-of-list/res.js) | Medium |
2626
| 22 | [Generate Parentheses](https://leetcode.com/problems/generate-parentheses/) | [JavaScript](./src/generate-parentheses/res.js) | Medium |
27+
| 24 | [swap-nodes-in-pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [TypeScript](./src/swap-nodes-in-pairs/res.ts) | Medium |
2728
| 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [JavaScript](./src/remove-duplicates-from-sorted-array/res.js) | Easy |
2829
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [JavaScript](./src/remove-element/res.js) | Easy |
2930
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [JavaScript](./src/implement-strstr/res.js) | Easy |

‎src/swap-nodes-in-pairs/res.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
*/
4+
// @ts-ignore
5+
class ListNode {
6+
val: number
7+
next: ListNode | null
8+
constructor(val?: number, next?: ListNode | null) {
9+
this.val = (val===undefined ? 0 : val)
10+
this.next = (next===undefined ? null : next)
11+
}
12+
}
13+
14+
function swapPairs(head: ListNode | null): ListNode | null {
15+
if (!head?.next) {
16+
return head;
17+
}
18+
19+
const result = head.next;
20+
let prevPoint = null;
21+
let currentPoint: ListNode | null = head;
22+
while (currentPoint) {
23+
const nextPoint = currentPoint.next;
24+
const restPoint = nextPoint?.next as ListNode | null;
25+
26+
if (nextPoint) {
27+
currentPoint.next = restPoint;
28+
nextPoint.next = currentPoint;
29+
if (prevPoint) {
30+
prevPoint.next = nextPoint;
31+
}
32+
prevPoint = currentPoint;
33+
currentPoint = restPoint;
34+
} else {
35+
break;
36+
}
37+
}
38+
39+
return result;
40+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /