慢慢找到对链表的感觉,加油保持前进呀哈哈哈哈!!!!
# 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