关闭

【Leetcode】Partition List

263人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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.

思路:

先把两类结点保存,然后统一链接。时间复杂度O(n),空间复杂度O(n)。

算法:

	public ListNode partition(ListNode head, int x) {
		ListNode p = head, h1 = null, r2 = null, r1 = null, h2 = null;
		List<Integer> l1 = new ArrayList<Integer>();
		List<Integer> l2 = new ArrayList<Integer>();
		while (p != null) {
			if (p.val < x) {
				l1.add(p.val);
			} else {
				l2.add(p.val);
			}
			p = p.next;
		}
		Iterator<Integer> i1 = l1.iterator();
		while (i1.hasNext()) {
			if (h1 == null) {
				h1 = new ListNode((Integer) i1.next());
				r1 = h1;
			} else {
				r1.next = new ListNode((Integer) i1.next());
				r1 = r1.next;
			}
		}
		Iterator<Integer> i2 = l2.iterator();
		while (i2.hasNext()) {
			if (h2 == null) {
				h2 = new ListNode((Integer) i2.next());
				r2 = h2;
			} else {
				r2.next = new ListNode((Integer) i2.next());
				r2 = r2.next;
			}
		}
		if (r1 != null) {
			r1.next = h2;
			head = h1;
		} else {
			head = h2;
		}

		return head;
	}



思路二:

将两类各自结点链接,最后合并。时间复杂度O(n),空间复杂度O(1)

算法二:

	public ListNode partition(ListNode head, int x) {
		ListNode lessHead = new ListNode(0);
		ListNode greaterHead = new ListNode(0);
		ListNode node = head, less = lessHead, greater = greaterHead;
		while (node != null) {
			ListNode next = node.next;
			if (node.val < x) {
				less.next = node;
				less = less.next;
				less.next = null;
			} else {
				greater.next = node;
				greater = greater.next;
				greater.next = null;
			}
			node = next;
		}
		less.next = greaterHead.next;
		return lessHead.next;
	}



0
0
查看评论

Partition List -- LeetCode

原题链接: http://oj.leetcode.com/problems/partition-list/  这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面。我们仍然是使用链表最常用的双指针大法,一个指向当前小于x的最后一个元素,一个进行往前扫描。如果元素大于x,...
  • linhuanmars
  • linhuanmars
  • 2014-04-25 04:51
  • 9191

leetcode_86_Partition List

思路: 刚开始试着把所有小于x的结点依次插到前面去,但是因为第一个和最后结点的问题真的把我搞得焦头烂额,后来想想,用我媳妇想到的方法可能更清晰一点,用两个链表分别连接小于和大于等于x 的结点,然后再把两个结点链接到一起,就可以了。在实施的时候稍微偷点懒,首先创建两个头节点,哎,现在终于明白头节点的...
  • dfb198998
  • dfb198998
  • 2015-02-05 21:57
  • 1052

【leetcode】【86】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&...
  • xiaoliucool1314
  • xiaoliucool1314
  • 2016-03-03 11:16
  • 680

LeetCode 86(Partition List)java

原题: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 ...
  • Jassy1994
  • Jassy1994
  • 2016-11-24 16:02
  • 430

【LeetCode】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...
  • ljiabin
  • ljiabin
  • 2014-12-12 11:21
  • 6212

LeetCode 86 Partition List (Python详解及实现)

【题目】 Given a linked list and a value x,partition it such that all nodes less than x come before nodes greater than orequal to x.   You should ...
  • yangjingjing9
  • yangjingjing9
  • 2017-08-09 17:15
  • 118

【LEETCODE】86- Partition List [Python]

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 ...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-02-26 10:46
  • 1127

[leetcode] 416. Partition Equal Subset Sum 解题报告

题目链接:https://leetcode.com/problems/partition-equal-subset-sum/ Given a non-empty array containing only positive integers, find if the...
  • qq508618087
  • qq508618087
  • 2016-10-10 04:43
  • 3815

LeetCode 416 Partition Equal Subset Sum

题目:Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum...
  • liuyue910828
  • liuyue910828
  • 2016-10-11 22:15
  • 1145

86. Partition List leetcode list

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 shoul...
  • shanshanhi
  • shanshanhi
  • 2016-12-25 15:24
  • 156
    个人资料
    • 访问:568257次
    • 积分:8668
    • 等级:
    • 排名:第2676名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论