leetcode - 86. Partition List

这篇具有很好参考价值的文章主要介绍了leetcode - 86. Partition List。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Description

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.

You should preserve the original relative order of the nodes in each of the two partitions.

Example 1:
leetcode - 86. Partition List,OJ题目记录,leetcode,list,算法

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]

Example 2:

Input: head = [2,1], x = 2
Output: [1,2]

Constraints:

The number of nodes in the list is in the range [0, 200].
-100 <= Node.val <= 100
-200 <= x <= 200

Solution

Use two list node and two tail node for smaller nodes and bigger nodes. Every time add the node to new linked lists.

Time complexity: o ( n ) o(n) o(n)
Space complexity: o ( 1 ) o(1) o(1)文章来源地址https://www.toymoban.com/news/detail-651448.html

Code

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def partition(self, head: Optional[ListNode], x: int) -> Optional[ListNode]:
        header_smaller, header2 = ListNode(-1), ListNode(-1)
        tail1, tail2 = header_smaller, header2
        p = head
        while p:
            if p.val < x:
                tail1.next = p
                tail1 = tail1.next
            else:
                tail2.next = p
                tail2 = tail2.next
            p = p.next
        tail1.next = header2.next
        tail2.next = None
        return header_smaller.next

到了这里,关于leetcode - 86. Partition List的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 206. Reverse Linked List

    Given the head of a singly linked list, reverse the list, and return the reversed list.   Example 1: Input head = [1,2,3,4,5] Output [5,4,3,2,1] Example 2: Input head = [1,2] Output [2,1] Example 3: Input head = [] Output [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 = Node.val = 5000 From: LeetCode Link: 206. Reverse Linked

    2024年02月01日
    浏览(49)
  • LeetCode //C - 141. Linked List Cycle

    Given head , the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a p

    2024年02月11日
    浏览(47)
  • LeetCode142. Linked List Cycle II

    Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to (0-indexed). It

    2024年02月04日
    浏览(40)
  • 【python】Leetcode(primer-dict-list)

    更多有关 dict 的相关背景和 leetcode 题解可参考: 【Programming】 【python】dict(7) 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。 示例 : 输入: [1,2,1,3,2,5] 输出: [3,5] 注意: 结果输出的顺序并不重要,对于上

    2024年02月11日
    浏览(38)
  • 【set】个人练习-Leetcode-817. Linked List Components

    题目链接:https://leetcode.cn/problems/linked-list-components/description/ 题目大意:给出一个 vectorint nums ,其中有一些数字。再给出一个链表的头指针 head ,链表内的元素各不相同。如果链表中有某一段(长度大于等于1)的元素都在 nums 中出现过,那么就算一个component,求链表中的co

    2024年02月13日
    浏览(40)
  • LeetCode //C - 328. Odd Even Linked List

    Given the head of a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and return the reordered list. The first node is considered odd, and the second node is even, and so on. Note that the relative order inside both the even and odd groups should remain as it was in the input. You must solve the proble

    2024年01月25日
    浏览(44)
  • LeetCode83. Remove Duplicates from Sorted List

    Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 = Node.val = 100 The list is guaranteed t

    2024年01月18日
    浏览(48)
  • LeetCode //C - 92. Reverse Linked List II

    Given the head of a singly linked list and two integers left and right where left = right, reverse the nodes of the list from position left to position right , and return the reversed list.   Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of

    2024年02月11日
    浏览(43)
  • 【LeetCode】设计数据结构 | List、Stack、Queue、DLinkedList

    设计链表(中等) 707. 设计链表 冗余版 代码复用简化版 用栈实现队列(简单) 232. 用栈实现队列 用队列实现栈(简单) 225. 用队列实现栈 方法一:双队列实现 方法二:单队列实现 设计循环队列(中等) 622. 设计循环队列 使用数组实现 使用链表实现 设计循环双端队列(中

    2024年02月14日
    浏览(34)
  • LeetCode 92. Reverse Linked List II【链表,头插法】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(41)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包