## [LeetCode][python3]0025. Reverse Nodes in k-Group

Start the journey

N2I -2020.03.19

- My first solution

`class Solution:`

def reverseKGroup(self, head: ListNode, k: int) -> ListNode:

if not head or k<2:

return head

seq=[0]

p1=head

index=1

while p1!=None:

seq.append(p1)

p1=p1.next

if index%k==0 and index!=0:

#print("swap",index-k+1,index+1)

seq[index-k+1:index+1]=seq[index:index-k:-1]

index+=1

h=p1=ListNode(0)

for i in seq[1:]:

p1.next=i

p1=p1.next

#print(i.val)

p1.next=None

return h.next

Note:

`seq[6:3:-1]`

Reverse order of`seq[4:7]`

, where`seq[6]`

is included and`seq[3]`

is not.

`print("example of s[a:b:-1]")`

s=[0,1,2,3,4,5]

print(s[1:4])

#>>[1,2,3]

print(s[3:0:-1])

#>>[3,2,1]

print(s[3:0:-2])

#>>[3,1]

print(s[0:4])

#>>[0,1,2,3]

print(s[3::-1])

#>>[3,2,1,0]

print(s[4:-1:-1]) #but -1 still represent len(s) here

#>>[]

Explanation:

Same way to solve it with Q.24 and others.