86. Partition List

来源:互联网 发布:淘宝好的丰胸产品 编辑:程序博客网 时间:2024/06/06 11:20

难度指数:Medium

题目要求:

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即为value。找到这个节点,然后将其右边所有比它小的节点置于左边大于x(如果有)的前面。如果没有,则置于x的前面

使用双链表,简单,不用多说。

public ListNode partition(ListNode head, int x) {        if(head == null || head.next == null)            return head;        ListNode lessList = new ListNode(0);        ListNode lessHead = lessList;        ListNode thanList = new ListNode(0);        ListNode thanHead = thanList;        while(head != null) {            if(head.val < x) {                lessHead.next = new ListNode(head.val);                lessHead = lessHead.next;            }            else {                thanHead.next = new ListNode(head.val);                thanHead = thanHead.next;               }            head = head.next;        }        lessHead.next = thanList.next;        return lessList.next;    }

更好的方式,在原有空间上穿针引线,再次证明了虚拟头节点的威力。

自己尝试的代码,对于contantNode和大于该节点的值的节点总是不能很好的串联的起来。导致失败,会形成一个环路。

    public ListNode partition(ListNode head, int x) {        if (head == null)            return null;        // 首先在找该节点的过程中        ListNode dummyNode = new ListNode(0);        dummyNode.next = head;        head = dummyNode;        ListNode preNode = head;        ListNode constantNode = null;        ListNode tempNode = head.next;        while (tempNode != null) {            if (tempNode.val >= x) {                constantNode = tempNode;                tempNode = tempNode.next;                break;            }            preNode = tempNode;            tempNode = tempNode.next;        }        if (tempNode == null)            return head.next;        boolean flag = false;        while (tempNode != null) {            if (tempNode.val < x) {                ListNode node = tempNode.next;                preNode.next = tempNode;                tempNode.next = null;                preNode = tempNode;                tempNode = node;            } else {                if (!flag) {                    constantNode.next = tempNode;                    flag = true;                }                tempNode = tempNode.next;            }        }        if (!flag)            constantNode.next = null;        preNode.next = constantNode;        return head.next;    }


改进:跳跃式穿针引线,和http://blog.csdn.net/m0_37751106/article/details/78872976这个问题有异曲同工之妙,看来解决问题的方式都是相同的。


链表就看这一个实现方式就够了!!!!

 public ListNode partition(ListNode head, int x) {        if (head == null || head.next == null)            return head;        // 首先在找该节点的过程中        ListNode dummyNode = new ListNode(0);        ListNode preNode = new ListNode(0);        ListNode curentNode = new ListNode(0);        dummyNode.next = preNode;        preNode.next = head;        curentNode.next = head;        while(curentNode.next != null && curentNode.next.val < x) {            preNode = preNode.next;            curentNode = curentNode.next;        }        while(curentNode.next != null) {            if(curentNode.next.val < x) {                ListNode temp = curentNode.next;                curentNode.next = curentNode.next.next;                temp.next = preNode.next;                preNode.next = temp;                preNode = temp;            } else {                curentNode = curentNode.next;            }        }       // 首节点的位置!设计巧妙。       return dummyNode.next.next;    }