Java for LeetCode 086

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.

解题思路:

只需记录小于3的最后一个指针和大于3的第一个指针即可,另外,如有创建一个ListNode result指向head可以减少代码长度,这个给出最简单的JAVA实现:

public ListNode partition(ListNode head, int x) {
		if (head == null || head.next == null)
			return head;
		ListNode temp = head, firstMin = head;
		if (head.val >= x) {
			while (firstMin.next != null) {
				if (firstMin.next.val < x) {
					head = firstMin.next;
					firstMin.next = firstMin.next.next;
					head.next = temp;
					break;
				}
				firstMin = firstMin.next;
			}
		}
		if (head.val >= x)
			return head;
		firstMin = head;
		temp=head.next;

		while (temp != null && temp.val < x) {
			firstMin = firstMin.next;
			temp = temp.next;
		}
		if(temp==null)
			return head;
		ListNode firstMax=temp,lastMax=temp;
		temp=temp.next;
		while(temp!=null){
			if(temp.val<x){
				firstMin.next=temp;
				firstMin=firstMin.next;
			}else{
				lastMax.next=temp;
				lastMax=lastMax.next;
			}
			temp=temp.next;
		}
		firstMin.next=firstMax;
		lastMax.next=null;
		return head;
	}
时间: 2024-10-14 02:01:17

Java for LeetCode 086的相关文章

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

Java for LeetCode 108 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If nums = [1,2,2], a soluti

Java for LeetCode 214 Shortest Palindrome

Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. For example: Given "aacecaaa", return "aaacecaaa&qu