新博文地址:Partition List
果然出去玩了一个星期回来状态极差。。。。
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.
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.
码代码时,脑子都不会动了。。。纯粹靠一步步调试AC的。囧啊,找到待分割点,将分割点后所有的小节点,插入分割点当做后驱,并维护分割位置tail。返回时,有一个坑,比如2,1 (x = 2),head = 2,将1插入到2前面时,返回head肯定是不对的,要返回分割点seperator->1,2。我特么在说些什么啊。o(╯□╰)o
等第二遍刷的时候,再把代码优化一下吧
public ListNode partition(ListNode head, int x) { if (head == null || head.next == null) { return head; } ListNode seperator = new ListNode(0); seperator.next = head; boolean isSplit = true; while (seperator.next != null) { if (seperator.next.val >= x) { break; } seperator = seperator.next; isSplit = false; } ListNode tem = seperator; ListNode tail = seperator; while (tem.next != null) { if (tem.next.val < x) { ListNode node = tem.next; tem.next = node.next; node.next = tail.next; tail.next = node; tail = node; } if (tem.next != null && tem.next.val < x) { continue; } else { tem = tem.next; } if (tem == null) { break; } } return isSplit ? seperator.next : head; }
相关推荐
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
这是LeetCode中Linked List所有题目的参考代码。(截止到目前为止:2015年12月14日)。
leetcode中文版
vs code LeetCode 插件
大佬的leetcode刷题笔记(c++版本)
Leetcode-List-Generator Node.js脚本可基于标签以csv格式生成问题列表(DS-Algo)
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
list-leetcode DEPRECATED(不再维护) 相关功能已并入,此仓库不再维护。 简介 输入你的leetcode账号密码,就能立即生成包含题号、标题、链接、难度、总提交数、总通过数、通过率、是否付费和已解决等内容的,所有...
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101_源码.zip
100个leetCode详细解答
文件中包含了LeetCode中Tag为LinkedList的题目参考代码。
LeetCode 刷题汇总1
LeetCode 选讲1
terminal-leetcode, 终端Leetcode是基于终端的Leetcode网站查看器 终端 leetcode终端leetcode是基于终端的leetcode网站查看器。本项目是由 RTV 激发的。 我最近正在学习本地化的反应,以实践我的新知识,并根据这个...
leetcode刷题, 直接用leetcode的分类方式.
该分类为结合《算法导论》的内容,给出Leetcode题目分类。题目主要集中在Leetcode的前400题中,也包括有后面的一些经典值得刷的题。该题目分类按照算法和数据结构排版,即可供单独Leetcode刷题使用,也可以配合学习...
LeetCode面试笔试题
leetcode高频面试笔试题150+道,亲身总结。
这份文档列出了leetcode几乎所有题目(大约134题)的分类以及难度指示,是刷leetcode的必备良品。现在leetcode总的题目数已经达到150题,所以有部分题目没有包含在这个文档中,但是——足够了。po主刷leetcode的时候...
LeetCode 刷题笔记