【LeetCode】Partition List 链表划分

来源:互联网 发布:手机视频录播软件 编辑:程序博客网 时间:2024/06/09 20:26

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.

即:给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前。
你应该保留两部分内链表节点原有的相对顺序。

样例
给定链表 1->4->3->2->5->2->null,并且 x=3

返回 1->2->2->4->3->5->null

标签
两根指针 链表
相关题目
中等 数组划分 30 %

(1)Java

/** * 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) {        // write your code here        if(head == null){            return null;//coding style!!!        }        ListNode leftDummy = new ListNode(0);        ListNode rightDummy = new ListNode(0);        ListNode left = leftDummy,  right = rightDummy;        while(head != null){            if(head.val < x){                left.next = head;                left = head;            }else{                right.next = head;                right = head;            }            head = head.next;        }        right.next = null;        left.next = rightDummy.next;        return leftDummy.next;    }}

(2)C++

#include "stdafx.h"#include <iostream>#include <cstdio>using namespace std;typedef struct tagSNode{    int val;     tagSNode* pNext;    tagSNode(int v) : val(v), pNext(NULL) {}}SNode;void Print(SNode* sn) {    SNode* p;    p = sn->pNext;    while (p)    {        if (p->pNext != NULL) {            cout << p->val << " -> ";        }        else {            cout << p->val;        }        p = p->pNext;    }    cout << endl;}void Destroy(SNode* p) {    SNode* next/* = p->pNext*/;    while (p)    {        next = p->pNext;        delete p;        p = next;//此处不可以直接p = p->pNext (×)!!就算此句位于delete p之前,也错!(因为这样改变了指针p的位置!)        //最好的方法:定义临时指针temp(is analogous to 'SNode* temp')    }}void Partition(SNode* pHead, int pivotKey) {if (pHead == NULL) {//Coding style!!!        return;    }    //两链表的头指针    SNode* pLeftpHead = new SNode(0);    SNode* pRightHead = new SNode(0);    //两链表的当前最后一个元素    SNode* left = pLeftpHead;    SNode* right = pRightHead;    SNode* p = pHead->pNext;    while (p)//遍历原链表    {        if (p->val < pivotKey) {            left->pNext = p;            left = p;//指针后移:等价于left = left->pNext;        }        else {            right->pNext = p;            right = p;//指针后移:等价于 right =  right->pNext;        }        p = p->pNext;    }    //将right后置空; 将right部分(的头指针后面部分)连接到left的尾部    right->pNext = NULL;    left->pNext = pRightHead->pNext;//不是right!保护表头结点!    //将整理好的链表赋给当前链表头部!!!    pHead->pNext = pLeftpHead->pNext;    delete pLeftpHead;    delete pRightHead;}int main(){    SNode* pHead = new SNode(0);    pHead->pNext = NULL;    for (int i = 0; i < 10; i++) {        SNode* p = new SNode(rand() % 10);        p->pNext = pHead->pNext;        pHead->pNext = p;    }    Print(pHead);    Partition(pHead, 3);    Print(pHead);    Destroy(pHead);    return 0;}