zoukankan      html  css  js  c++  java
  • leetcode——86. 分隔链表

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

    # 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
     

    public ListNode partition(ListNode head, int x) {
            if(head == null || head.next == null){
                return head;
            }
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode p = dummy;
            ListNode s = head;
            ListNode t = dummy;
            while (s != null && s.val < x){
                s = s.next;
                t = t.next;
                p = p.next;
            }
            while (s != null){
                if(s.val >= x){
                    s = s.next;
                    t = t.next;
                }else{
                    t.next = s.next;
                    s.next = p.next;
                    p.next = s;
                    p = p.next;
                    s = t.next;
                }
            }
            return dummy.next;
        }

    比较简单

     ——2020.7.14

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    Postman post csrf_token
    CBV
    nginx+uWSGI+django部署web服务器
    JS绘图
    get_字段_display()
    limit_choices_to
    window.onload=function(){};
    模拟百度搜索项目
    事件冒泡
    解绑事件
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11745603.html
Copyright © 2011-2022 走看看