lintcode 96 链表划分

来源:互联网 发布:单片机简易电子琴7键 编辑:程序博客网 时间:2024/06/05 11:24
1.

给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。

你应该保留两部分内链表节点原有的相对顺序。

2.完全没有思路..........

3./**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */
class Solution {
public:
    
        ListNode *partition(ListNode *head, int x) {
ListNode * small = new ListNode(0); //小于x
ListNode * large = new ListNode(0); //大于x
ListNode * lastSmall = small,* lastLarge = large;
ListNode * cur = head;
while(cur){
if(cur->val < x){
lastSmall->next = cur;
lastSmall =  lastSmall->next;
}else{
lastLarge->next = cur;
lastLarge =  lastLarge->next;
}
cur = cur->next;
lastSmall->next = lastLarge->next = NULL; 
}
lastSmall->next = large->next;
return small->next;
    }
};

4.感想

完全不会啊这道题·.有种百度看了答案都不懂得感觉..问问同学.也感觉模模糊糊....

只能先放下这道题.等在学习后再回来看看.


0 0