[LeetCode]86.Partition List

160 篇文章 28 订阅
【题目】

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的所有节点之前。 
你应该保持在每两个分区的节点的原始相对顺序。

【分析】

【代码】

/*********************************
*   日期:2014-01-28
*   作者:SJF0115
*   题目: 86.Partition List
*   网址:http://oj.leetcode.com/problems/partition-list/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        ListNode *leftHead = (ListNode*)malloc(sizeof(ListNode));
        ListNode *rightHead = (ListNode*)malloc(sizeof(ListNode));
        ListNode *lpre = leftHead,*rpre = rightHead;
        while(head != NULL){
            if(head->val < x){
                lpre->next = head;
                lpre = head;
            }
            else{
                rpre->next = head;
                rpre = head;
            }
            head = head->next;
        }
        rpre->next = NULL;
        lpre->next = rightHead->next;
        return leftHead->next;
    }
};
int main() {
    Solution solution;
    int A[] = {1,3,2};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 3;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.partition(head->next,5);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    return 0;
}


【温故】

/*-------------------------------------------------------------------
*   日期:2014-04-10
*   作者:SJF0115
*   题目: 86.Partition List
*   来源:https://leetcode.com/problems/partition-list/
*   结果:AC
*   来源:LeetCode
*   总结:
--------------------------------------------------------------------*/
class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if(head == nullptr){
            return nullptr;
        }//if
        ListNode *left = new ListNode(-1);
        ListNode *right = new ListNode(-1);
        ListNode *p = left,*q = right,*cur = head;
        while(cur){
            if(cur->val < x){
                p->next = cur;
                p = cur;
            }//if
            else{
                q->next = cur;
                q = cur;
            }//else
            cur = cur->next;
        }//while
        q->next = NULL;
        p->next = right->next;
        return left->next;
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@SmartSi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值