python algorithm diary (list series) _leetcode 86. delimited list

Given a list and a particular value x, to the list are separated so that all the nodes are less than x is greater than or equal to the previous node x. Initial relative position of the two partitions should you keep each node.

Example:

Input: head = 1-> 4-> 3- > 2-> 5-> 2, x = 3
Output: 1-> 2-> 2-> 4-> 3-> 5

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/partition-list

Most began to think x is the location, a bit confused. Found later by x is given a value of x node.
Stupid idea is to use list:

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

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        if not head: #写不写都无所谓
            return
        res = []
        while(head):
            res.append(head.val)
            head = head.next
        dummy = ListNode(0) #装比x小的节点
        dummy1 = ListNode(0) #装比x大的节点
        cur = dummy
        curr = dummy1
        for i in res:
            if i<x:
                dummy.next = ListNode(i)
                dummy = dummy.next
            else:
                dummy1.next = ListNode(i)
                dummy1 = dummy1.next
        dummy.next = curr.next
        return cur.next

For space complexity, and to cut list:

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

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        if not head: #可以不写
            return
        dummy = ListNode(0) #装小的
        dummy1 = ListNode(0) #装大的
        cur = dummy
        curr = dummy1
        while(head):
            if head.val<x:
                dummy.next = head
                dummy = dummy.next
            else:
                dummy1.next = head
                dummy1 = dummy1.next
            head = head.next
        dummy1.next=None  #注意删大的最后的尾巴
        dummy.next = curr.next
        return cur.next

This idea is similar to the previous one, delete the big points, even on dummy1. dummy would retain only a small

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

class Solution:
    def partition(self, head: ListNode, x: int) -> ListNode:
        if not head:
            return
        dummy = ListNode(0) #装值小于x的节点
        dummy1 = ListNode(0) #装值大于x的节点
        cur = dummy
        curr = dummy1
        dummy.next = head
        while(dummy.next):
            if dummy.next.val>=x:
                dummy1.next = dummy.next  #先连接后删除
                dummy.next = dummy.next.next
                dummy1 = dummy1.next
            else:
                dummy = dummy.next
        dummy1.next=None  # 注意把最后大于x节点的尾巴删掉
        dummy.next = curr.next
        return cur.next

 

Published 44 original articles · won praise 0 · Views 1903

Guess you like

Origin blog.csdn.net/weixin_39331401/article/details/104614810