资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

PartitionList-创新互联

描述

十多年的资中网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整资中建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“资中网站设计”,“资中网站推广”以来,每个客户项目都认真落实执行。

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.

partition_list.h

#include 
#include 
#include 

using namespace std;

typedef struct ListNode {
    int _val;
    ListNode *_next;
    ListNode(int x) : _val(x), _next(NULL)
    {}  
}node,*node_p;

class Solution
{
public:
    node_p partition(node_p &list,int x)
    {   
        //参数检查
        if(list==NULL)
            return NULL;
     
        node dummy(-1);
        node_p head=&dummy;
        head->_next=list;
        
        node_p first=head;
        
        //找_val为x的结点
        node_p node_x=list;
        while(node_x!=NULL && node_x->_val!=x){
            node_x=node_x->_next;
            first=first->_next;
        }
        if(node_x==NULL){
            cout<<"not find node_x!"<_next;
        while(tmp){
            if(tmp->_val_next=tmp->_next;
                tmp->_next=node_x;
                first->_next=tmp;
                first=first->_next;
                tmp=node_x->_next;
            }else{
                tmp=tmp->_next;
                prev=prev->_next;
            }
        }
        return head->_next;
    }

    //构造一个链表
    node_p create_list(int *array,int size)
    {
        assert(array);

        node dummy(-1);
        node_p prev=&dummy;
        node_p head=prev;
        for(int i=0;i_next=Node;
            prev=prev->_next;
        }
        return head->_next;
    }
};

partition_list.cpp

#include "partition_list.h"

using namespace std;

int main()
{
    int array[]={1,4,3,2,5,2};
    Solution s1; 
    node_p list=s1.create_list(array,sizeof(array)/sizeof(array[0]));
    node_p newlist=s1.partition(list,4);
    
    node_p tmp=newlist;
    while(tmp){
        cout<_val<<"  ";
        free(tmp);
        tmp=tmp->_next;
    }   
    cout<

运行结果:

Partition List

下面为leetcode实现的代码:

Partition List

《完》

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


分享文章:PartitionList-创新互联
链接分享:http://cdkjz.cn/article/dsgcjh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220