链表-Partition List

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的更多相关文章
- [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 ...
- leetcode 链表 Partition List
Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions Given a linked list and ...
- LeetCode 86. 分隔链表(Partition List)
86. 分隔链表 86. Partition List 题目描述 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的 ...
- Leetcode解题思想总结篇:双指针
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...
- LeetCode_算法及数据结构覆盖统计
[输入]共计151道题的算法&数据结构基础数据 (见附录A) [输出-算法]其中有算法记录的共计 97道 ,统计后 结果如下 top3(递归,动态规划,回溯) 递归 动态规划 回溯 BFS ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode总结【转】
转自:http://blog.csdn.net/lanxu_yy/article/details/17848219 版权声明:本文为博主原创文章,未经博主允许不得转载. 最近完成了www.leetco ...
- leetcode解题文件夹
点击打开链接点击打开链接点击打开链接參考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 只是本文准备用超链接的方式连接到对应解答页面 ...
- [LeetCode] Partition List 划分链表
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
随机推荐
- python---连接MySQL第一页
前言:python如果想要连接到MySQL要安装上相关的驱动才下:好在驱动程序在mysql的官网可以下载的到. 以下是我自己保存的一个conetos7版本 http://yunpan.cn/cLACS ...
- linux 如何禁用账号和解除禁用账号
把账号禁用可以有几个方法:1. # usermod -L <username> # usermod -U <username> // 解除禁用2. 修改/etc/passwd文 ...
- 解决 MySQL manager or server PID file could not be found! 的方法
[root@centos var]# service mysqld stop MySQL manager or server PID file could not be found! [F ...
- POJ1007
2014-08-22 题目意思: 按照各个字符串的逆序数排序(稳定排序,即若A=B,则AB的顺序还是原来的样子) 思路: 求出每个字符串的逆序数后,排序输出即可 代码: //Memory Time ...
- 京东区块排版负margin用法
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Web API Login with Cookie
public HttpWebResponse InitiliazeWebRequest() { string responseData =string.Empty;//string url = Get ...
- 使用Vitamio打造自己的Android万能播放器(3)——本地播放(主界面、播放列表)
前言 打造一款完整可用的Android播放器有许多功能和细节需要完成,也涉及到各种丰富的知识和内容,本章将结合Fragment.ViewPager来搭建播放器的主界面,并实现本地播放基本功能.系列文章 ...
- 随滚动条滚动的居中div
<!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Http请求头中的字段理解
1.Accept属于请求头, Content-Type属于实体头. Http报头分为通用报头,请求报头,响应报头和实体报头. 请求方的http报头结构:通用报头|请求报头|实体报头 响应方的http报 ...
- TCP/IP详解之:ARP协议 和 RARP协议
ARP功能:从逻辑internet地址(IP地址)到对应的物理硬件地址(以太网地址)之间的转换 ARP工作原理: (1)首先每个主机都会在自己的ARP缓冲区中建立一个ARP列表,以表示IP和MAC间的 ...