mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
@ -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
|
||||
|
Reference in New Issue
Block a user