leetcode 143. reorder list

wonderful world·2021년 12월 23일
0

leetcode

목록 보기
12/21
# 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     
profile
hello wirld

0개의 댓글