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