mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
修改(0024.两两交换链表中的节点.md):优化typescript版本代码,增强易读性
This commit is contained in:
@ -254,20 +254,19 @@ TypeScript:
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function swapPairs(head: ListNode | null): ListNode | null {
|
function swapPairs(head: ListNode | null): ListNode | null {
|
||||||
const dummyHead: ListNode = new ListNode(0, head);
|
const dummyNode: ListNode = new ListNode(0, head);
|
||||||
let cur: ListNode = dummyHead;
|
let curNode: ListNode | null = dummyNode;
|
||||||
while(cur.next !== null && cur.next.next !== null) {
|
while (curNode && curNode.next && curNode.next.next) {
|
||||||
const tem: ListNode = cur.next;
|
let firstNode: ListNode = curNode.next,
|
||||||
const tem1: ListNode = cur.next.next.next;
|
secNode: ListNode = curNode.next.next,
|
||||||
|
thirdNode: ListNode | null = curNode.next.next.next;
|
||||||
cur.next = cur.next.next; // step 1
|
curNode.next = secNode;
|
||||||
cur.next.next = tem; // step 2
|
secNode.next = firstNode;
|
||||||
cur.next.next.next = tem1; // step 3
|
firstNode.next = thirdNode;
|
||||||
|
curNode = firstNode;
|
||||||
cur = cur.next.next;
|
}
|
||||||
}
|
return dummyNode.next;
|
||||||
return dummyHead.next;
|
};
|
||||||
}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Kotlin:
|
Kotlin:
|
||||||
|
Reference in New Issue
Block a user