Partition List

来源:互联网 发布:大数据呼叫中心 编辑:程序博客网 时间:2024/06/08 18:20

题目

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

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 的值,再链接起来。时间O(N)

注意:要将链表的尾结点赋为 NULL ,否则会形成环链表。

tmps->next = NULL;

tmpb->next = NULL;

代码一

/** * 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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        ListNode *small = NULL;        ListNode *big = NULL;        ListNode *cur = head;        ListNode *tmps = NULL;        ListNode *tmpb = NULL;        while(cur)        {            if(cur->val<x)            {                if(small==NULL)                {                    small = cur;                    tmps = cur;                }                else                {                    tmps->next = cur;                    tmps = tmps->next;                }                cur = cur->next;                tmps->next = NULL;                                }            else            {                if(big==NULL)                {                    big = cur;                    tmpb = cur;                }                else                {                    tmpb->next = cur;                    tmpb = tmpb->next;                }                cur = cur->next;                tmpb->next = NULL;            }          }        if(tmps)        {            tmps->next = big;            return small;        }        else            return big;                       }};


代码二

注意:tmpb->next = NULL;  不可缺少,否则形成环形链表。

/** * 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) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        ListNode *small = NULL;        ListNode *big = NULL;        ListNode *cur = head;        ListNode *tmps = NULL;        ListNode *tmpb = NULL;        while(cur)        {            if(cur->val<x)            {                if(small==NULL)                {                    small = cur;                    tmps = cur;                }                else                {                    tmps->next = cur;                    tmps = tmps->next;                }                               }            else            {                if(big==NULL)                {                    big = cur;                    tmpb = cur;                }                else                {                    tmpb->next = cur;                    tmpb = tmpb->next;                }            }              cur = cur->next;        }        if(tmps && tmpb)        {            tmpb->next = NULL;            tmps->next = big;            return small;        }        if(tmps)            return small;        if(tmpb)            return big;            }};


 代码三

可以通到先申请两个空节点作为头节点

class Solution {public:    ListNode *partition(ListNode *head, int x) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        ListNode small(0);        ListNode large(0);        ListNode* pSmall = &small;        ListNode* pLarge = &large;        while (head != NULL) {            if (head->val < x) {                pSmall->next = head;                pSmall = head;            }            else {                pLarge->next = head;                pLarge = head;            }            head = head->next;        }                pSmall->next = large.next;        pLarge->next = NULL;        return small.next;     }};

 

原创粉丝点击