LEETCODE: Partition List
来源:互联网 发布:slack for mac 编辑:程序博客网 时间:2024/06/16 06:24
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(head == NULL || head->next == NULL) return head; ListNode *newHead = NULL; ListNode *smaller = NULL; ListNode *largehead = NULL; ListNode *larger = NULL; while(head != NULL) { if(head->val < x) { if(smaller == NULL) { smaller = head; newHead = head; } else { smaller->next = head; smaller = smaller->next; } } else { if(larger == NULL) { larger = head; largehead = head; } else { larger->next = head; larger = larger->next; } } head = head->next; } if(larger != NULL) { larger->next = NULL; } if(smaller != NULL) { smaller->next = largehead; return newHead; } return largehead; }};
Partition List
0 0
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- [Leetcode] Partition List
- [LeetCode] Partition List
- leetcode 81: Partition List
- [Leetcode] Partition List
- leetcode 118: Partition List
- [LeetCode]Partition List
- [leetcode]Partition List
- LeetCode-Partition List
- [leetcode] Partition List
- LeetCode - Partition List
- [LeetCode] Partition List
- LeetCode 86: Partition List
- LeetCode:Partition List
- 【leetcode】Partition List
- 2-2-3 嵌套和级联的判断-比较数的大小(从2个数比较到3个数)?
- UML类图和对象图
- java解惑之表达式之谜(谜题2)
- mkdir -p 参数,可以直接创建层级的目录,不需要创建一个进入一个
- 【连通图|强连通+缩点】POJ-2186 Popular Cows
- LEETCODE: Partition List
- angularJS的输入验证
- 剑指offer 面试题33 数组组合的最小数
- 企业如何确定自己的网络营销核心产品?
- 为什么莫名其妙的就被调用了
- cocos2dxandroid运行Luac编译后的lua代码
- WebService学习系列(三)------XML-RPC
- python V2.7.6 Documentation Built-in Functions
- 上传多个文件直接把内容转存到本地