diff --git a/problems/0143.重排链表.md b/problems/0143.重排链表.md index 00622623..790bcb48 100644 --- a/problems/0143.重排链表.md +++ b/problems/0143.重排链表.md @@ -336,7 +336,33 @@ class Solution: return pre ``` ### Go - +```go +# 方法三 分割链表 +func reorderList(head *ListNode) { + var slow=head + var fast=head + for fast!=nil&&fast.Next!=nil{ + slow=slow.Next + fast=fast.Next.Next + } //双指针将链表分为左右两部分 + var right =new(ListNode) + for slow!=nil{ + temp:=slow.Next + slow.Next=right.Next + right.Next=slow + slow=temp + } //翻转链表右半部分 + right=right.Next //right为反转后得右半部分 + h:=head + for right.Next!=nil{ + temp:=right.Next + right.Next=h.Next + h.Next=right + h=h.Next.Next + right=temp + } //将左右两部分重新组合 +} +``` ### JavaScript ```javascript