*Leetcode 86. Partition List
来源:互联网 发布:数据异构拆分系统 编辑:程序博客网 时间:2024/05/22 11:32
https://leetcode.com/submissions/detail/105588330/
链表partition 为了不让自己绕进去,所以写的时候没考虑长度,按最清晰的思路写。不怕变量多。。。
/** * 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) { ListNode *bigger = NULL, *new_head = NULL, *bigger_ptr = NULL, *new_head_ptr = NULL; if (head == NULL) return head; while (head) { if (head -> val >= x) { if (bigger == NULL) { bigger_ptr = bigger = head; } else { bigger_ptr ->next = head; bigger_ptr = bigger_ptr->next; } } else { if (new_head == NULL) { new_head_ptr = new_head = head; } else { new_head_ptr ->next = head; new_head_ptr = new_head_ptr->next; } } head = head->next; } if (bigger_ptr) bigger_ptr ->next = NULL; if (new_head_ptr == NULL) { return bigger; } else { new_head_ptr -> next = bigger; return new_head; } } // ListNode* partition(ListNode* head, int x) { // ListNode *bigger = NULL, *cur=NULL, *new_head = head; // if (head == NULL) return head; // if (head->next == NULL) return head; // while( head -> next ) { // if (((head -> next) -> val) >= x) { // if (bigger == NULL) { // cur = bigger = head->next; // } else { // cur -> next = head->next; // cur = cur -> next; // } // } // if (head ->next -> next != NULL) { // head = head->next; // } else { // break; // } // } // head->next = bigger; // return new_head; // }};
阅读全文
0 0
- [LeetCode]86.Partition List
- LeetCode 86. Partition List
- [Leetcode] 86. Partition List
- LeetCode --- 86. Partition List
- [leetcode] 86.Partition List
- [leetcode] 86. Partition List
- [LeetCode]86. Partition List
- 86. Partition List LeetCode
- Leetcode 86. Partition List
- LeetCode *** 86. Partition List
- LeetCode 86. Partition List
- [LeetCode]86. Partition List
- LeetCode 86. Partition List
- leetcode 86. Partition List
- Leetcode 86. Partition List
- LeetCode-86.Partition List
- [Leetcode]86. Partition List
- LeetCode - 86. Partition List
- 解决一直Gradle Build Running的问题
- 插入排序
- 树莓派3 启动蓝牙no default controller
- Java Web开发
- 服务器完全配置 (Part 1)
- *Leetcode 86. Partition List
- Jsoup组件:学习之Whitelist类
- 服务器完全配置 (Part 2)
- 《数据结构学习与实验指导》3-6:表达式转换
- 服务器完全配置 (Part 3)
- SharedPreferences的简单使用
- 服务器完全配置 (Part 4)
- 关于HTTP解析的一点思考
- C++ 15 —— 多态