Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions problems/0143.重排链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public:
cur = head;
int i = 1;
int j = vec.size() - 1; // i j为之前前后的双指针
int count = 0; // 计数,偶数去后面,奇数取前面
int count = 0; // 计数,偶数取后面,奇数取前面
while (i <= j) {
if (count % 2 == 0) {
cur->next = vec[j];
Expand Down Expand Up @@ -73,7 +73,7 @@ public:
}

cur = head;
int count = 0; // 计数,偶数去后面,奇数取前面
int count = 0; // 计数,偶数取后面,奇数取前面
ListNode* node;
while(que.size()) {
if (count % 2 == 0) {
Expand Down Expand Up @@ -338,8 +338,85 @@ class Solution:
return pre
```
### Go

```go
// 方法一 数组模拟
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reorderList(head *ListNode) {
vec := make([]*ListNode, 0)
cur := head
if cur == nil {
return
}
for cur != nil {
vec = append(vec, cur)
cur = cur.Next
}
cur = head
i := 1
j := len(vec) - 1 // i j为前后的双指针
count := 0 // 计数,偶数取后面,奇数取前面
for i <= j {
if count % 2 == 0 {
cur.Next = vec[j]
j--
} else {
cur.Next = vec[i]
i++
}
cur = cur.Next
count++
}
cur.Next = nil // 注意结尾
}
```

```go
// 方法二 双向队列模拟
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reorderList(head *ListNode) {
que := make([]*ListNode, 0)
cur := head
if cur == nil {
return
}

for cur.Next != nil {
que = append(que, cur.Next)
cur = cur.Next
}

cur = head
count := 0 // 计数,偶数取后面,奇数取前面
for len(que) > 0 {
if count % 2 == 0 {
cur.Next = que[len(que)-1]
que = que[:len(que)-1]
} else {
cur.Next = que[0]
que = que[1:]
}
count++
cur = cur.Next
}
cur.Next = nil // 注意结尾
}
```

```go
# 方法三 分割链表
// 方法三 分割链表
func reorderList(head *ListNode) {
var slow=head
var fast=head
Expand Down