struct ListNode* partition(struct ListNode* head, int x) {
struct ListNode *p1=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *p2=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *p=head; struct ListNode *r1=p1;
struct ListNode *r2=p2; while(p)
{
if(p->val<x)
{
r1->next=p;
r1=p;
}
else
{
r2->next=p;
r2=p;
}
p=p->next;
}
r2->next=NULL;
r1->next=p2->next; return p1->next;
}

思路就是申请两个新的指针p1和p2,小于x的链表结点添加在p1后面,大于x的链表结点添加在p2后面,最后,将p2链接在p1后面即可。要注意p1和p2需带有各自的头结点,这样处理起来会方便一些,最后链接时去掉头结点即可。

链表-Partition List的更多相关文章

  1. [Swift]LeetCode86. 分隔链表 | Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. leetcode 链表 Partition List

    Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and ...

  3. LeetCode 86. 分隔链表(Partition List)

    86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...

  4. Leetcode解题思想总结篇:双指针

    Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...

  5. LeetCode_算法及数据结构覆盖统计

    [输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下  top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...

  6. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  7. LeetCode总结【转】

    转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetco ...

  8. leetcode解题文件夹

    点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...

  9. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

随机推荐

  1. python 网络编程第三版

    为服务端增加多线程解决方案 1.服务端代码如下: ***这个版本并没有真正的起到多线程的作用,主要原因在于t.join():以后的版本会改进这个问题*** #!/usr/bin/python #!co ...

  2. 可爱的 Python : Python中函数式编程,第二部分

    英文原文:Charming Python: Functional programming in Python, Part 2,翻译:开源中国 摘要:  本专栏继续让David对Python中的函数式编 ...

  3. grep 基于关键字搜索

    grep 'linux' /etc/passwd 搜索passwd文件下的包含linux的行 find / -user linux|grep Video 在用户为linux的根目录下搜房Video内容 ...

  4. 图片文件,图片文件流和BASE64加密字符串之间的转换,以及图片的BASE64加密字符串再jsp上如何显示

    http://blog.csdn.net/sidongxue2/article/details/43036373

  5. 二叉查找树的Insert和Delete操作

    struct TreeNode{ SearchTree Left; SearchTree Right; ElementType Ele; }; /*递归一定有出口*/ /*递归代码就是要重复使用*/ ...

  6. hdu 5578 Friendship of Frog(multiset的应用)

    Problem Description N frogs . Two frogs are friends if they come from the same country. The closest ...

  7. python代码中pass的用法

    我们有时会在方法中写一些注释代码,用来提示这个方法是干嘛的之类,看下面代码: class Game_object: def __init__(self, name): self.name = name ...

  8. Nagios利用NSClient++监控Windows主机

    在Nagios的libexec下有check_nt这个插件,它就是用来检查windows机器的服务的.其功能类似于check_nrpe.不过还需要搭配另外一个软件NSClient++,它则类似于NRP ...

  9. NotificationManager 发送通知

    该应用的界面如下,界面代码在此不再给出,源码github账户下载 MainActivity.java public class MainActivity extends Activity { priv ...

  10. C++_nullptr

    C++_nullptr null 0 nullptr 的区别