86.分隔链表

难度:中等
题目描述:
86.分隔链表_第1张图片
思路总结:这个题为啥是中等题?没有什么大规模的用例,也没有什么特别的东西。将节点分到两个链表上,然后最后合并,就这么简单暴力。
题解一:

# 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 head
        smaller = ListNode(0)
        res = smaller
        bigger = ListNode(0)
        bh = bigger
        while head:
            if head.val < x:
                smaller.next = head
                smaller = smaller.next
            else:
                bigger.next = head
                bigger = bigger.next
            tmp = head
            head = head.next
            tmp.next = None
        print()
        smaller.next = bh.next
        return res.next

题解一结果:
86.分隔链表_第2张图片

你可能感兴趣的:(朱滕威的面试之路)