diff --git a/solution/0000-0099/0025.Reverse Nodes in k-Group/README.md b/solution/0000-0099/0025.Reverse Nodes in k-Group/README.md index 7d7b68ed8d508..16a2249e6d3d1 100644 --- a/solution/0000-0099/0025.Reverse Nodes in k-Group/README.md +++ b/solution/0000-0099/0025.Reverse Nodes in k-Group/README.md @@ -50,6 +50,14 @@ +**方法一:迭代** + +时间复杂度为 $O(n)$,空间复杂度为 $O(1)$,其中 $n$ 是链表的长度。 + +**方法二:递归** + +时间复杂度为 $O(n)$,空间复杂度为 $O(\log _k n)$,其中 $n$ 是链表的长度。 + ### **Python3** @@ -244,6 +252,8 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null { ### **Go** +迭代: + ```go /** * Definition for singly-linked list. @@ -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 } ``` diff --git a/solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md b/solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md index b0edef40caf7e..4f3439c1c331c 100644 --- a/solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md +++ b/solution/0000-0099/0025.Reverse Nodes in k-Group/README_EN.md @@ -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)$. + ### **Python3** @@ -229,6 +237,8 @@ function reverseKGroup(head: ListNode | null, k: number): ListNode | null { ### **Go** +Iteration: + ```go /** * Definition for singly-linked list. @@ -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 } ``` diff --git a/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go b/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go index f83d477d7d097..870ed21329463 100644 --- a/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go +++ b/solution/0000-0099/0025.Reverse Nodes in k-Group/Solution.go @@ -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 -} \ No newline at end of file + return pre +}