LeetCode: Partition List [086]

【题目】

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.

【题意】

给定一个链表和一个数值x,将链表中的值按x进行划分,小于x的在前,大于等于x的在后。两部分中节点的之间的相对位置与在原链表中时相同

【思路】

先将链表按x分裂成两个链表,一个链表中的值小于x, 另一个链表中的值大于等于x

然后在将两个链表链接起来。

【代码】

/**
 * 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 head;

        ListNode*headLess=NULL;
        ListNode*tailLess=NULL;
        ListNode*headGreater=NULL;
        ListNode*tailGreater=NULL;
        ListNode*pointer=head;
        while(pointer){
            if(pointer->val<x){
                if(tailLess==NULL)headLess=pointer;
                else tailLess->next=pointer;
                tailLess=pointer;
                pointer=pointer->next;
                tailLess->next=NULL;
            }
            else{
                if(tailGreater==NULL)headGreater=pointer;
                else tailGreater->next=pointer;
                tailGreater=pointer;
                pointer=pointer->next;
                tailGreater->next=NULL;
            }
        }
        //合并两个链表
        if(tailLess){
            head=headLess;
            tailLess->next=headGreater;
        }
        else head=headGreater;

        return head;
    }
};

LeetCode: Partition List [086],布布扣,bubuko.com

时间: 2024-08-05 23:40:41

LeetCode: Partition List [086]的相关文章

Leetcode:Partition List 链表快速排序划分

Partition List 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

[LeetCode] [Partition List 2012-04-30]

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

[LeetCode]Partition List

题目描述:(链接) 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

LeetCode: Partition List解题报告

Partition List 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, Give

[LeetCode] Partition Equal Subset Sum 相同子集和分割

Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal. Note: Both the array size and each of the array element will not exceed 100. Exam

[LeetCode] Partition List 划分链表

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

leetcode:Partition Array by odd and even

1. Partition an integers array into odd number first and even number second. Given [1, 2, 3, 4], return [1, 3, 2, 4] 2.思路 1.通过两次遍历,不合算. 2.一次遍历,一个从头,一个从尾,如果碰到偶数,兑换位置,此方法为排序. 3. public void partitionArray(int[] nums) { int start = 0, end = nums.length

LeetCode: Partition List 解题报告

Partition List 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

LeetCode Partition Equal Subset Sum

原题链接在这里:https://leetcode.com/problems/partition-equal-subset-sum/description/ 题目: Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.