86. Partition List (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.

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if(!head || !(head->next) ) return head;
        ListNode *current = head;
        ListNode *smallPointer = NULL; //point to the last node <x
        ListNode *largePointer = NULL; //point to the last node >x
        while(current)
        {
            if(current->val >= x)
            {
                largePointer = current;
                current = current->next;
            }
            else
            {
                if(!largePointer)
                {
                    smallPointer = current;
                    current = current->next;
                }
                else if(smallPointer)
                {
                    largePointer->next = smallPointer->next;
                    smallPointer -> next = current;
                    current = current->next;
                    smallPointer = smallPointer->next;
                    smallPointer->next = largePointer->next;
                    largePointer->next = current;
                }
                else //head
                {
                    smallPointer = current;
                    current = current->next;
                    smallPointer->next = head;
                    head = smallPointer;
                    largePointer->next = current;
                }
            }
        }
        return head;

    }
};
时间: 2024-12-15 06:05:58

86. Partition List (List)的相关文章

LeetCode --- 86. 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,

86.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

[leedcode 86] 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 解题思路

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

86. Partition List java solutions

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

[LeedCode OJ]#86 Partition List

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/partition-list/ 题意: 给定一个链表和一个x,要求在不改变其在原本链表中相对位置的情况下,将小于x的结点放在新链表的左边,大于等于x的结点放在新链表的右边 思路: 思路很简单,新建两个链表,一个存放大于等于x的结点,一个存放小于x的结点,左后再合并两个链表即可. /** * Definition for s

leetCode 86.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->

【一天一道LeetCode】#86. Partition List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 origin

LeetCode OJ 86. 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