python leetcode 143. Reorder List

考察对链表的操作

class Solution(object):
    def reorderList(self, head):
        """
        :type head: ListNode
        :rtype: void Do not return anything, modify head in-place instead.
        """
        if not head or not head.next:
            return 
        count=0
        p=head
        res=[]
        while p:
            res.append(p)
            count+=1 
            p=p.next
        count=count>>1 
        p=head
        while count>0:
            tail=res.pop()
            r=p.next
            p.next=tail
            tail.next=r 
            p=r
            count-=1
        p.next=None

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84997761