首页 > 其他 > 详细

leetcode——86. 分隔链表

时间:2019-10-26 21:28:23      阅读:129      评论:0      收藏:0      [点我收藏+]

慢慢找到对链表的感觉,加油保持前进呀哈哈哈哈!!!!

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def partition(self, head, x):
        """
        :type head: ListNode
        :type x: int
        :rtype: ListNode
        """
        if not head:
            return 
        a=ListNode(-1)
        a.next=head
        p=head
        r=p
        q=p.next
        if p.val>=x:
            p=a
        while q:
            if q.val>=x:
                q=q.next
                r=r.next
            else:
                if q==p.next:
                    p=p.next
                    r=r.next
                    q=q.next
                else:
                    r.next=q.next
                    q.next=p.next
                    p.next=q
                    p=q
                    q=r.next
        return a.next
执行用时 :20 ms, 在所有 python 提交中击败了91.08%的用户
内存消耗 :11.8 MB, 在所有 python 提交中击败了26.74%的用户
 
——2019.10.26

leetcode——86. 分隔链表

原文:https://www.cnblogs.com/taoyuxin/p/11745603.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!