File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
solution/0025.Reverse Nodes in k-Group Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * type ListNode struct {
4
+ * Val int
5
+ * Next *ListNode
6
+ * }
7
+ */
8
+ func reverseKGroup (head * ListNode , k int ) * ListNode {
9
+ start := head
10
+ var h * ListNode = nil
11
+ var t * ListNode = nil
12
+ ok := false
13
+ if h , t , ok = revert (start , k ); ! ok {
14
+ return head
15
+ }
16
+ r := h
17
+ p := t
18
+ print (2 , r )
19
+ for ok {
20
+ start = t .Next
21
+ h , t , ok = revert (start , k )
22
+ p .Next = h
23
+ p = t
24
+ }
25
+ return r
26
+ }
27
+
28
+ func revert (head * ListNode , k int ) (h , t * ListNode , ok bool ) {
29
+ c := head
30
+ if ! check (c , k ) {
31
+ return head , nil , false
32
+ }
33
+
34
+ t = head
35
+ var p * ListNode = nil
36
+ for k > 0 {
37
+ temp := head
38
+ head = head .Next
39
+ temp .Next = p
40
+ p = temp
41
+ k --
42
+ }
43
+ h = p
44
+ t .Next = head
45
+ ok = true
46
+ print (11 , h )
47
+ return
48
+ }
49
+
50
+ func check (head * ListNode , k int ) bool {
51
+ for k > 0 {
52
+ if head == nil {
53
+ return false
54
+ }
55
+ k --
56
+ head = head .Next
57
+ }
58
+ return true
59
+ }
You can’t perform that action at this time.
0 commit comments