Partition List
来源:互联网 发布:itunes更新软件 编辑:程序博客网 时间:2024/04/29 06:14
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
.
做链表题需格外注意特殊情况
/** * 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) { if (0 == head ) return head; ListNode dummy(0), *pre = &dummy, *cur = &dummy, *tmp; dummy.next = head; while(cur->next != 0) { if (cur->next->val < x ) { if (cur == pre) { cur = cur->next; pre = pre->next; continue; } tmp = cur->next; cur->next = tmp->next; tmp->next = pre->next; pre->next = tmp; pre = pre->next; } else cur = cur->next; } return dummy.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
- homeworkday05
- Python3.2官方文档翻译--作用域和命名空间实例
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(九) ServerSocketChannel
- Java NIO系列教程(十) Java NIO DatagramChannel
- Partition List
- B. Sereja and Mirroring
- Java NIO系列教程(十一) Pipe
- CSU 1414: Query on a Tree
- Java NIO系列教程(十二) Java NIO与IO
- Sicily 6774
- 三层架构
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Servlet的线程安全问题