[Lintcode] Partition 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 should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given 1->4->3->2->5->2->null and x = 3,
return 1->2->2->4->3->5->null.

SOLUTION:

其实就是快速排序嘛,比较简单,按照题意实现就对了,开两个头节点,一个放小于x的,一个放大于x的,再拼起来。

注意,注意,注意,链表的最后一定要指向null,就是右边的链表尾部一定要指向null!!!

代码:

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @param x: an integer
     * @return: a ListNode
     */
    public ListNode partition(ListNode head, int x) {
        if (head == null){
            return head;
        }
        ListNode dummyLeft = new ListNode(0);
        ListNode dummyRight = new ListNode(0);
        ListNode left = dummyLeft;
        ListNode right = dummyRight;
        while (head != null){
            if (head.val < x){
                left.next = head;
                left = left.next;
            } else {
                right.next = head;
                right = right.next;
            }
            head = head.next;
        }
        left.next = dummyRight.next;
        right.next = null;
        return dummyLeft.next;
    }
}

时间: 2024-11-12 11:52:55

[Lintcode] Partition List的相关文章

LintCode &quot;Partition Array by Odd and Even&quot;

One pass in-place solution: all swaps. class Solution { public: /** * @param nums: a vector of integers * @return: nothing */ void partitionArray(vector<int> &nums) { size_t len = nums.size(); int i = 0, io = 0, ie = len - 1; while (io < ie)

lintcode 容易题:Partition List 链表划分

题目: 链表划分 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前. 你应该保留两部分内链表节点原有的相对顺序. 样例 给定链表 1->4->3->2->5->2->null,并且 x=3 返回 1->2->2->4->3->5->null 解题: 上面返回的结果好多不对的应该是: 1->2->2->3->4->5->null 想了好久不知道怎么搞,九章看到的程序,竟然搞

【Lintcode】096.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. Example Given 1->4->3->2

Lintcode31 Partition Array solution题解

[题目描述] Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:All elements < k are moved to the left;All elements >= k are moved to the right;Return the partitioning index, i.e the fir

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道.在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单.要求找到2个数组的交集,简单点的方法就是用2个hashSet,第一个HashSet存第一个数组的元素.然后扫描第二个数组,如果第二个数组中的元素在第一个HashSet中出现了,那么就把它加到第二个HashSet中.最后第二个HashSet就是两个数组的交集了. 2)138. Subarray Sum 要求在一个数组中找到一个子数

Lintcode: Kth Largest Element 解题报告

Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3,2,4,8], the 3th largest element is 4 Challenge O(n) time, O(1) space 原题链接: http://www.lintcode.com/en/problem/kth-largest-element

LintCode链表题总结

由于链表本身结构的单一性,链表的题目很少会有很大的变种,基本都是围绕几个基本的考点出题目.所以链表的题目比较好掌握,但是链表的题目又不太容易一次就AC通过,由于边界情况未考虑.空指针(比如head.next不存在但是却给head.next赋值了,就会抛出nullpointer的错误).越界等边界情况,我们需要在测试用例的时候多考虑边界条件.在模拟计算的时候一定要用纸和笔把中间的操作过程给画出来,这样比较容易形成思路. 在LintCode的ladder1中,链表那一章有如下这一些题目: 此外,Li

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param