LeetCode笔记

链表划分

2018-05-02  本文已影响14人  只为此心无垠

题目地址
两个注意点
1、dummy node
2、more.next = None 记得较大的链表的next值为none,有可能成为循环链表

def partition(self, head, x):
        
        # write your code here
        
        if head == None or head.next == None:
            return head
        
        head_little = ListNode(0)
        head_more = ListNode(0)
        little = head_little
        more = head_more
        
        while head:
            if head.val < x:
                little.next = head
                little = head
            else:
                more.next = head
                more = head
            head = head.next
        more.next = None
        little.next = head_more.next
        
        return head_little.next
上一篇 下一篇

猜你喜欢

热点阅读