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

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    html5不能播放视频的方法
    mysql找出重复数据的方法
    jquery each循环遍历完再执行的方法
    Android:TextView跑马灯-详解
    日志处理(一) log4j 入门和详解(转)
    周记 2014.11.08
    周记 2014.11.01
    linux下解压命令大全
    关于Context []startup failed due to previous errors
    周记 2014.10.25
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/11745603.html
Copyright © 2011-2022 走看看