leetcode || 86、Partition List

problem:

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.

Hide Tags

Linked List Two
Pointers

题意:类似于快速排序的第一步,将乱序的链表分成两部分,小于target的放在前面,不小于target的放后面

thinking:

(1)利用快速排序的思想,将小于目标值的结点前移。但是,由于快速排序是不稳定的,所以不满足题目稳定性的要求,若套用快速排序必然是错的

(2)那就改造快速排序。快速排序的双指针游走方向是相反的,这就导致了排序的不稳定性。如果双指针同时沿着递增的方向游走,则是稳定的。

(3)对于本题,除了游走的双指针,还要增加前驱指针和最后一个小于目标值的后驱指针

code:

改造快速排序:

/**
 * 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) {
         ListNode *p=NULL;         //左指针
         ListNode *after=NULL;     //指向第一个不小于x的结点
         ListNode *pre=head;     //q的前驱结点
         ListNode *q=head;        //右指针
         while(q!=NULL)
         {
             ListNode *tmp=q->next;
             if(q->val<x)
             {
                 if(after==NULL)   //从第一个结点小于x
                 {
                     if(p==NULL)
                     {
                         p=q;
                         head=p;
                     }
                     else
                         p=p->next;  //不用移动
                 }
                 else   //第一个结点大于X
                 {
                     if(p==NULL)
                     {
                         p=q;
                         head=p;
                         p->next=after;
                         if(after->next==q)  //p q之间只有一个结点不小于X
                            pre=after;
                         pre->next=tmp;
                     }
                     else
                     {
                         p->next=q;
                         p=p->next;
                         p->next=after;
                         if(after->next==q)    //p q之间只有一个结点不小于X
                            pre=after;
                         pre->next=tmp;
                     }
                 }

             }
             else
             {
                 if(after==NULL)
                     after=q;
                 pre=q;   //连续结点不小于X
             }
             q=tmp;
         }
         return head;
     }
 };
时间: 2024-10-05 06:16:09

leetcode || 86、Partition List的相关文章

LeetCode - 86、分隔链表

给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4->3->2->5->2, x = 3    输出: 1->2->2->4->3->5 1 /** 2 * 列表定义 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode

【Leetcode 86】 Partition List

问题描述: 给定一个list, 将所有小于x的node放到左边,剩下的保持原样. 问题解决: 闲的无聊,用c++和python都做了一遍. 代码如下: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def partition(self, head, x)

(LeetCode 86)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 86】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 --- 86. 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,

解决iphone5,5s有锁版(AU,SB,S版等等)ios7越狱后+86、FT、IM等一切问题

最近无聊,给大家发一个关于完美解决iphone5,5c.5s有锁版本机号码.+86.短信.facetime.imessage等问题.是ios7系统哦!(本人亲测iphone5 SB版 双模卡解锁)相当完美!!!1.先越狱!必须的!!2.打开cydia,增加源(cydia.china3gpp.com)3.增加源后,进入,点(iphone5s/5c/5 ios7 gpp),安装.4.完工! 也许有其他方法解决此问题,比如修改系统文件覆盖此类的,看着都头疼,但这个是最简单的哦! 解决iphone5,5

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值

leetcode || 119、Pascal&#39;s Triangle II

problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? Hide Tags Array 题意:输出杨辉三角形的第K层 即:第K+1行 thinking: 题目要求使用O(K)的额外空间

[LeetCode] 86. 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