博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
86. Partition List
阅读量:4646 次
发布时间:2019-06-09

本文共 1421 字,大约阅读时间需要 4 分钟。

题目:

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.

链接: 

6/4/2017

审题不清,题中只要和target一样大或者比target大的都在比target小的后面,所以不需要按照小,一样大,大这种来区分,只要按照小,不小就可以了。

注意第31行,这是为了避免出现环状输出。large.next应该是null

1 /** 2  * Definition for singly-linked list. 3  * public class ListNode { 4  *     int val; 5  *     ListNode next; 6  *     ListNode(int x) { val = x; } 7  * } 8  */ 9 public class Solution {10     public ListNode partition(ListNode head, int x) {11         if (head == null) {12             return head;13         }14         ListNode dummySmall = new ListNode(-1);15         ListNode dummyLarge = new ListNode(-1);16 17         ListNode cur = head;18         ListNode small = dummySmall;19         ListNode large = dummyLarge;20 21         while (cur != null) {22             if (cur.val < x) {23                 small.next = cur;24                 small = small.next;25             } else {26                 large.next = cur;27                 large = large.next;28             }29             cur = cur.next;30         }31         large.next = null;32         small.next = dummyLarge.next;33         return dummySmall.next;34     }35 }

更多讨论:

转载于:https://www.cnblogs.com/panini/p/6942576.html

你可能感兴趣的文章
数据集
查看>>
打印python包含汉字报SyntaxError: Non-ASCII character '\xe4' in file
查看>>
[Leetcode] unique paths ii 独特路径
查看>>
HDU 1217 Arbitrage (Floyd + SPFA判环)
查看>>
IntelliJ idea学习资源
查看>>
Django Rest Framework -解析器
查看>>
ExtJs 分组表格控件----监听
查看>>
Hibernate二级缓存配置
查看>>
LoadRunner常用术语
查看>>
关于jedis2.4以上版本的连接池配置,及工具类
查看>>
记忆讲师石伟华微信公众号2017所有文章汇总(待更新)
查看>>
mechanize (1)
查看>>
FactoryBean
查看>>
Coolite动态加载CheckboxGroup,无法在后台中获取
查看>>
如何在我们项目中利用开源的图表(js chart)
查看>>
nfs服务器工作原理
查看>>
C3P0连接池工具类使用
查看>>
SVN常用命令备注
查看>>
孩子教育
查看>>
解决Cacti监控图像断断续续问题
查看>>