leetcode 143. reorder list
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
def as_array(l):
r=[]
while(l.next != None):
r.append(l)
l=l.next
r.append(l)
return r
xs = as_array(head)
ys = list(reversed(xs))
h=len(xs)//2
for i in range(h):
xs[i].next=ys[i]
ys[i].next=xs[i+1]
if h%2!=0:
xs[h].next=None
else:
ys[h-1].next=xs[h]
xs[h].next=None
Reference
この問題について(leetcode 143. reorder list), 我々は、より多くの情報をここで見つけました https://velog.io/@jhcho/leetcode-143.-reorder-listテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol