其他分享
首页 > 其他分享> > 面试题 02.04. 分割链表

面试题 02.04. 分割链表

作者:互联网

# 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:

        #双链表法
        pmin = node1 = ListNode(0)
        pmax = node2 = ListNode(0)
        while head:
            if head.val < x:
                pmin.next = head
                pmax = pmin.next
            else:
                pmax.next = head
                pmax = pmax.next
            head = head.next
        pmin.next = node2
        pmax.next = None
        return node1

标签:pmin,面试题,ListNode,02.04,self,head,next,链表,pmax
来源: https://blog.csdn.net/qq_41209110/article/details/120394140