Partition List
来源:互联网 发布:prada高仿包淘宝 编辑:程序博客网 时间:2024/06/07 06:42
题目: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.
分析:链表的特点在于不存在固定大小的存储区域,节点可以任意移动,插入。算法:遍历链表,分别插入两个新链表,一个小于x,一个大于等于x。然后把后者拼接在前者之后。
ListNode *partition(ListNode *head, int x) { if(!head||!head->next) return head; ListNode dummy_left(-1),dummy_right(-1); ListNode *cur_left = &dummy_left, *cur_right = &dummy_right; while(head) { if(head->val<x) { cur_left->next=head; cur_left=head; } else { cur_right->next=head; cur_right=head; } head=head->next; } cur_left->next=dummy_right.next; cur_right->next= NULL; return dummy_left.next; }
0 0
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Nginx设置alias实现虚拟目录 alias与root的用法区别
- linux命令
- Struts2学习笔记(九) OGNL
- hdu-oj 1862 EXCEL排序
- 天津落马公安局长拥35项发明 被查抄12辆车物品
- Partition List
- csu oj 1395: Timebomb(读取用*组成的数字)
- kvhhjkghkjhklhjkhjkhj
- 【MFC 定义消息、设置鼠标状态】
- android 闹钟的实现
- Netflix逆袭:从失足青年到硅谷精英
- java 汉字转换为拼音 简码 及全拼音
- Unix网络编程:while中执行waitpid为什么能正确解决同时出现的多个子进程结束
- Zedboard & Zynq 图像采集 视频开发 (五) 板端Linux下视频TCP发送 & Linux上位机QT视频显示