`
cozilla
  • 浏览: 91878 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Partition List

 
阅读更多

Partition List

 
AC Rate: 428/1694
My Submissions

 

Given 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.

For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if (head == NULL) return NULL;
        ListNode lessHead(99), largeHead(99);
        ListNode* less = &lessHead, *large = &largeHead, *cur = head;
        while (cur != NULL) {
            if (cur->val < x) {
                less->next = cur;
                less = cur;
            } else {
                large->next = cur;
                large = cur;
            }
            cur = cur->next;
        }
        large->next = NULL;
        less->next = largeHead.next;
        return lessHead.next;
    }
};

 

分享到:
评论

相关推荐

    python-leetcode题解之086-Partition-List

    python python_leetcode题解之086_Partition_List

    js-leetcode题解之86-partition-list.js

    javascript js_leetcode题解之86-partition-list.js

    c语言-leetcode题解之0086-partition-list.zip

    c语言基础 c语言_leetcode题解之0086_partition_list.zip

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    Leetcode题目+解析+思路+答案.pdf

    - **Partition List**:将链表按值分割成两个部分。 - **Add Two Numbers**:两个非负整数相加,结果存储在链表中。 - **Copy List with Random Pointer**:复制带有随机指针的链表。 8. **数学(Math)**: - ...

    leetcode中文版-LeetCode:力码

    86.Partition List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160....

    Leetcode答案(c++版)

    **1.8 Partition List (86)** - **问题描述**:给定一个链表和一个值 x,将链表中小于 x 的节点排在大于等于 x 的节点之前。 - **解题思路**: - 创建两个虚拟头节点分别记录小于 x 和大于等于 x 的节点。 - 遍历...

    leetcode中325题python-leetcode:leetcode

    partition-list 92 反转链表 II reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表 linked-list-cycle 142 环形链表 II linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文...

    leetcode-cpp刷题

    - **2.2.3 Partition List** - 按照给定值对链表进行分区。 - 实现思路:维护两个指针,分别指向小于和大于给定值的节点,最后连接两个部分。 - **2.2.4 Remove Duplicates from Sorted List** - 移除排序链表...

    python-leetcode面试题解之第86题分隔链表-题解.zip

    这段代码首先定义了一个`ListNode`类,然后实现了`partitionList`函数,按照上述策略进行链表分割。函数接受链表头节点作为输入,并返回两个新链表的头节点。 通过解决这个问题,你不仅可以提高对链表操作的理解,...

    leetcodelintcode差异-leetcode-python:leetcode-python

    leetcode lintcode差异 leetcode-python 九章算法基础班 二分 题目 地址 153. Find ...List) LintCode 373. Partition Array by Odd and Even Mock Interview 题目 Solution Tag Dynamic Programming

    leetcode338-LeetCode_practice:LeetCode_practice

    list , Reference: Stack , Reference: , , Heap , Tree , , , DP/Greedy Reference: Reference: , Reference: , , , , , Recurrence Reference: Important NOTE!: 其中的Partition 是很常见的operation,标准做法是...

    LeetCode练习答案

    - **划分链表(Partition List)**: 将链表中所有小于x的节点移到所有大于或等于x的节点之前。 - **两数相加(Add Two Numbers)**: 给定两个非空链表代表两个非负整数,数字以逆序方式存储,每一位节点包含一个数字,将...

Global site tag (gtag.js) - Google Analytics