添加(0143.重排链表.md):增加go版本

This commit is contained in:
xuerbujia
2022-04-12 17:45:50 +08:00
committed by GitHub
parent 7ba1a67c86
commit af724ef6d5

View File

@ -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