From af724ef6d5ed95c4ba545a932f8257b487648aec Mon Sep 17 00:00:00 2001 From: xuerbujia <83055661+xuerbujia@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:45:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880143.=E9=87=8D?= =?UTF-8?q?=E6=8E=92=E9=93=BE=E8=A1=A8.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0143.重排链表.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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