Skip to content

Commit 6cd58df

Browse files
committed
修改(0024.两两交换链表中的节点.md):优化typescript版本代码,增强易读性
1 parent 9c32528 commit 6cd58df

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

problems/0024.两两交换链表中的节点.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,19 @@ TypeScript:
254254

255255
```typescript
256256
function swapPairs(head: ListNode | null): ListNode | null {
257-
const dummyHead: ListNode = new ListNode(0, head);
258-
let cur: ListNode = dummyHead;
259-
while(cur.next !== null && cur.next.next !== null) {
260-
const tem: ListNode = cur.next;
261-
const tem1: ListNode = cur.next.next.next;
262-
263-
cur.next = cur.next.next; // step 1
264-
cur.next.next = tem; // step 2
265-
cur.next.next.next = tem1; // step 3
266-
267-
cur = cur.next.next;
268-
}
269-
return dummyHead.next;
270-
}
257+
const dummyNode: ListNode = new ListNode(0, head);
258+
let curNode: ListNode | null = dummyNode;
259+
while (curNode && curNode.next && curNode.next.next) {
260+
let firstNode: ListNode = curNode.next,
261+
secNode: ListNode = curNode.next.next,
262+
thirdNode: ListNode | null = curNode.next.next.next;
263+
curNode.next = secNode;
264+
secNode.next = firstNode;
265+
firstNode.next = thirdNode;
266+
curNode = firstNode;
267+
}
268+
return dummyNode.next;
269+
};
271270
```
272271

273272
Kotlin:

0 commit comments

Comments
 (0)