【链表】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.

思路:

只要把比x小的节点按顺序连成一条链,比x大或等于的节点连成另一条链,然后把两条链连起来。注意一下边界情况(某条链为空)。

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} x
 * @return {ListNode}
 */
var partition = function(head, x) {
    if(head==null||head.next==null){
        return head;
    }
    var lHead=null;
    var gHead=null;

    var p=head,pl=null,pg=null,temp=null;

    while(p){
        if(p.val<x){
            if(lHead==null){
                lHead=p;
                pl=p;
            }else{
                pl.next=p;
                pl=pl.next;
            }
        }else{
            if(gHead==null){
                gHead=p;
                pg=p;
            }else{
                pg.next=p;
                pg=pg.next;
            }
        }
        temp=p;
        p=p.next;
        temp.next=null;
    }

    if(pg!=null){
        pg.next=null;
    }
    if(lHead!=null){
        pl.next=gHead;
        return lHead;
    }else{
        return gHead;
    }
};
时间: 2024-10-24 15:29:53

【链表】Partition List的相关文章

leetcode 链表 Partition List

Partition List Total Accepted: 19761 Total Submissions: 73252My Submissions 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

leetcode 86. 分隔链表(Partition List)

目录 题目描述: 示例: 解法: 题目描述: 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1->4->3->2->5->2, x = 3 输出: 1->2->2->4->3->5 解法: /** * Definition for singly-linked list. * struct ListNode { *

leetcode解题目录

参考文献:http://blog.csdn.net/lanxu_yy/article/details/17848219 不过本文准备用超链接的方式连接到相应解答页面,不断更新中 题目 算法 数据结构 注意事项 Clone Graph BFS 哈希表 Word Ladder II BFS 哈希表 Surrounded Regions BFS 矩阵 Word Ladder BFS N/A Binary Tree Level Order Traversal BFS|前序遍历 队列 Binary Tre

Leetcode: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

LeetCode OJ: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]89. 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

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 想了好久不知道怎么搞,九章看到的程序,竟然搞

[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

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]86. Partition List分离链表

/* 这个题是medium的意思应该是用双指针的方法做,如果使用下边的新建链表的方法,就是easy的题目了 双指针会用到很多链表的相连操作 */ public ListNode partition(ListNode head, int x) { ListNode s = null; ListNode b = null; ListNode temp=null,res=null; while (head!=null) { int cur = head.val; if (head.val<x) { i