Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 5 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Dec 23, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
143.重排链表增加Go方法一和方法二
  • Loading branch information
markwang1992 committed Nov 12, 2024
commit 50472c381cd423ac12ff4c6eb8bb6126a4f5a2d1
83 changes: 80 additions & 3 deletions problems/0143.重排链表.md
View file Open in desktop
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

AltStyle によって変換されたページ (->オリジナル) /