LeetCode_Partition List
来源:互联网 发布:网络炒股的人多吗 编辑:程序博客网 时间:2024/05/22 19:57
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
.
代码:
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };ListNode* partition(ListNode* head, int x){ if(head==NULL) return NULL; ListNode* begin = head; ListNode* second = head->next; ListNode* second_pre = head; while(second!=NULL) { if(second->val < x) { if(begin==head&&begin->val >=x)//第一个节点就是比较大的节点 { ListNode* head1 = new ListNode(second->val); head1->next = head; head = head1; //删除second指向的节点 second_pre -> next = second -> next; second = second_pre->next; begin = head; } else if(second_pre->val < x)//连续两个都比较小 { second_pre = second; begin = second_pre; second = second->next; } else { second_pre->next = second->next; second->next = begin->next; begin->next = second; second = second_pre->next; begin = begin ->next; } } else { second_pre = second; second = second->next; } } return head;}
1 0
- Leetcode_partition-list
- Leetcode_Partition List
- LeetCode_Partition List
- LeetCode_Partition List
- List
- list
- List
- LIST
- list
- list
- List
- list
- list
- List
- list
- LIST
- List
- List
- unindent does not match any outer
- dwr学习1
- hdu 1754 I Hate It(线段树入门-单点更新)
- Leetcode(8):String to Integer (atoi)
- 谈C#中的Delegate
- LeetCode_Partition List
- 网页简单文件下载
- C++课程学习简单总结
- PAT(Advance) 1089. Insert or Merge (25)
- 从零开始打造一个新闻订阅APP之服务器篇(二、类时间片轮转算法+redis sorted set 实现“逛”功能)
- SQL循环语句
- vim map映射
- win7启动提示:bootMGR被压缩
- 进程关系