【Leetcode】Partition List (Swap)

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.

这道题是一道切割的题。要求把比x小的元素放到x的前面。并且相对顺序不能变

这个时候我们採用双指针法

runner1用于寻找最后一个比x小得元素,这里作为插入点(切割点)

runner2用于寻找应该插到插入点之后的元素

所以这里会出现三种情况

情况1: runner2的元素小于x。这个时候假设runner1和runner2指向同一个元素。说明还没有找到切割点,所以两个指针继续往前走即可了。

情况2: runner2的元素小于x。这个时候假设runner1和runner2指向不同的元素。说明runner1已经走到了切割点前,而runner2.next就是应该被插到切割点前。把runner2.next插入到切割点前就ok

情况3: runner2的元素大于x, 这个时候找到了切割点,仅仅移动runner2就能够了。直到runner2.next小于切割点。

然后依照情况2来操作就能够了

情况1和情况2:

			if (runner2.next.val < x) {
				if (runner1 == runner2) {
					runner1 = runner1.next;
					runner2 = runner2.next;
				} else {
					ListNode insert = runner2.next;
					ListNode next = insert.next;
					insert.next = runner1.next;
					runner1.next = insert;
					runner2.next = next;
					runner1 = runner1.next;
				}
			} 

情况3:

			else
				runner2 = runner2.next;

完整代码例如以下

	public ListNode partition(ListNode head, int x) {
		if (head == null)
			return head;

		ListNode helper = new ListNode(0);
		helper.next = head;
		ListNode runner1 = helper;
		ListNode runner2 = helper;

		while (runner2 != null && runner2.next != null) {
			if (runner2.next.val < x) {
				if (runner1 == runner2) {
					runner1 = runner1.next;
					runner2 = runner2.next;
				} else {
					ListNode insert = runner2.next;
					ListNode next = insert.next;
					insert.next = runner1.next;
					runner1.next = insert;
					runner2.next = next;
					runner1 = runner1.next;
				}
			} else
				runner2 = runner2.next;
		}
		return helper.next;
	}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-08-27 03:14:38

【Leetcode】Partition List (Swap)的相关文章

【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 x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2

【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 x. You should preserve the original relative order of the nodes in each of the two partitions. For example, Given 1->4->3

【leetcode】Partition List(middle)

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

【leetcode】N-queens

问题: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration

【leetcode】N-queens II

问题: 返回N皇后问题解的个数. 分析: 详见 N-queens 实现: bool nextPermutation(vector<int> &num) { int i = num.size() - 1; while (i >= 1) { if(num[i] > num[i - 1]) { --i; int ii = num.size() - 1; while (ii > i && num[ii] <= num[i]) --ii; if(ii &g

【Leetcode】Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

【leetcode】 First Missing Positive

[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 找到第一个没有出现的正整数 思路:

【LeetCode】数组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum [4]Median of Two Sorted Arrays [11]Container With Most Water [15]3Sum [16]3Sum Closest [18]4Sum [26]Remove Duplicates from Sorted Array [27]Remove Element [31]Next Permutatio