Skip to content

Commit

Permalink
feat: update go solutions to lc problem: No.0025
Browse files Browse the repository at this point in the history
No.0025.Reverse Nodes in k-Group
  • Loading branch information
poltao committed Nov 20, 2022
1 parent 9969c49 commit e304b36
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 88 deletions.
86 changes: 60 additions & 26 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:迭代**

时间复杂度为 $O(n)$,空间复杂度为 $O(1)$,其中 $n$ 是链表的长度。

**方法二:递归**

时间复杂度为 $O(n)$,空间复杂度为 $O(\log _k n)$,其中 $n$ 是链表的长度。

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -244,6 +252,8 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null {

### **Go**

迭代:

```go
/**
* Definition for singly-linked list.
Expand All @@ -253,40 +263,64 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
dummy := &ListNode{0, head}
pre := dummy
cur := dummy
for cur.Next != nil {
for i := 0; i < k && cur != nil; i++ {
var dummy *ListNode = &ListNode{}
p, cur := dummy, head
for cur != nil {
start := cur
for i := 0; i < k; i++ {
if cur == nil {
p.Next = start
return dummy.Next
}
cur = cur.Next
}
if cur == nil {
return dummy.Next
}
t := cur.Next
cur.Next = nil
start := pre.Next
pre.Next = reverseList(start)
start.Next = t
pre = start
cur = pre
p.Next, p = reverse(start, cur), start
}
return dummy.Next
}

func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
}
dummyHead := &ListNode{}
cur := head
for cur != nil {
tmp := cur.Next
cur.Next = dummyHead.Next
dummyHead.Next = cur
cur = tmp
return pre
}
```

递归:

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
start, end := head, head
for i := 0; i < k; i++ {
if end == nil {
return head
}
end = end.Next
}
res := reverse(start, end)
start.Next = reverseKGroup(end, k)
return res
}

func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
}
return dummyHead.Next
return pre
}
```

Expand Down
98 changes: 66 additions & 32 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@

## Solutions

**Approach 1: Iteration**

Time complexity $O(n)$, Space complexity $O(1)$.

**Approach 2: Recursion**

Time complexity $O(n)$, Space complexity $O(\log _k n)$.

<!-- tabs:start -->

### **Python3**
Expand Down Expand Up @@ -229,6 +237,8 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null {

### **Go**

Iteration:

```go
/**
* Definition for singly-linked list.
Expand All @@ -238,40 +248,64 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null {
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
dummy := &ListNode{0, head}
pre := dummy
cur := dummy
for cur.Next != nil {
for i := 0; i < k && cur != nil; i++ {
cur = cur.Next
}
if cur == nil {
return dummy.Next
}
t := cur.Next
cur.Next = nil
start := pre.Next
pre.Next = reverseList(start)
start.Next = t
pre = start
cur = pre
}
return dummy.Next
var dummy *ListNode = &ListNode{}
p, cur := dummy, head
for cur != nil {
start := cur
for i := 0; i < k; i++ {
if cur == nil {
p.Next = start
return dummy.Next
}
cur = cur.Next
}
p.Next, p = reverse(start, cur), start
}
return dummy.Next
}

func reverseList(head *ListNode) *ListNode {
if head == nil ||head.Next == nil {
return head
}
dummyHead := &ListNode{}
cur := head
for cur != nil {
tmp := cur.Next
cur.Next = dummyHead.Next
dummyHead.Next = cur
cur = tmp
}
return dummyHead.Next
func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
}
return pre
}
```

Recursion:

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
start, end := head, head
for i := 0; i < k; i++ {
if end == nil {
return head
}
end = end.Next
}
res := reverse(start, end)
start.Next = reverseKGroup(end, k)
return res
}

func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
}
return pre
}
```

Expand Down
46 changes: 16 additions & 30 deletions solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,24 @@
* }
*/
func reverseKGroup(head *ListNode, k int) *ListNode {
dummy := &ListNode{0, head}
pre := dummy
cur := dummy
for cur.Next != nil {
for i := 0; i < k && cur != nil; i++ {
cur = cur.Next
start, end := head, head
for i := 0; i < k; i++ {
if end == nil {
return head
}
if cur == nil {
return dummy.Next
}
t := cur.Next
cur.Next = nil
start := pre.Next
pre.Next = reverseList(start)
start.Next = t
pre = start
cur = pre
end = end.Next
}
return dummy.Next
res := reverse(start, end)
start.Next = reverseKGroup(end, k)
return res
}

func reverseList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}
dummyHead := &ListNode{}
cur := head
for cur != nil {
tmp := cur.Next
cur.Next = dummyHead.Next
dummyHead.Next = cur
cur = tmp
func reverse(start, end *ListNode) *ListNode {
var pre *ListNode = nil
for start != end {
tmp := start.Next
start.Next, pre = pre, start
start = tmp
}
return dummyHead.Next
}
return pre
}

0 comments on commit e304b36

Please sign in to comment.