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

  1. 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
N2I -2020.03.19
  • 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
#>>[]

--

--

--

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Inversion of Control and Dependency Injection with Spring

Introducing Tplus

Inbound email Service for creating records in Salesforce

Positive Outlook of Error in Software Development

Kotlin extension

Introduction to Python Virtual Environment (venv)

How to make amiibo with iPhone in 2022

The programming superpower

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
N2I

N2I

HI I’m N2I. Now a SWE in Taiwan. Check out more about me in https://nzi2020.blogspot.com/ or contact via email: nayzi9999@gmail.com

More from Medium

Tail Recursion in Python

From Python To Java (Part 5) — Dictionaries (Python) VS HashMaps (Java)

np arange with Example — pythonpip.com

Pattern 6 : In-place Reversal of a LinkedList