【一天一道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 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.

(二)解题

题目大意:给定一个链表和一个值,让小于给定值的结点都移到链表前面来,但不能改变这些数在原链表中的相对位置。

解题思路:用两个指针,一个指向小于给定值的链表部分的尾结点,一个用于遍历链表。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        if(head==NULL||head->next==NULL) return head;
        ListNode* lessTail = head;//小于x部分的尾节点
        ListNode* p = head;
        ListNode* pre = NULL;//p前面的节点
        bool isFirst = false;//如果头节点就大于x为true,反之false
        bool needToChange = false;//p前面的节点都小于x,则不需要改变,反之需要进行插入操作
        if(head->val >= x) {isFirst = true;needToChange = true;}//如果第一个数就大于x,需要改变头节点
        while(p!=NULL)
        {
            if(p->val < x)
            {
                if(isFirst)//需要改变头节点
                {
                    ListNode* temp = p->next;
                    pre->next = p->next;
                    p->next=head;
                    lessTail = p;
                    head = p;
                    p = temp;
                    isFirst = false;
                }
                else
                {
                    if(!needToChange)//前面部分都小于x,则不需要改变
                    {
                        lessTail = p;
                        pre = p;
                        p=p->next;
                    }
                    else//需要进行插入操作
                    {
                        ListNode* temp = p->next;
                        pre->next = p->next;
                        p->next = lessTail->next;
                        lessTail->next = p;
                        lessTail = p;//注意要改变小于x部分的尾节点
                        p = temp;
                    }
                }
            }
            else
            {
                pre = p;
                p=p->next;
                needToChange = true;
            }
        }
        return head;
    }
};

后记:在这里祝大家端午节快乐,也给端午节还在坚持刷题的我自己点个赞。哈哈!

时间: 2024-09-30 04:55:10

【一天一道LeetCode】#86. Partition 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,

[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

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 Java

题目: 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分离链表

/* 这个题是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

[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. Input: head = 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. Example: Input: head = 1->4->3-

19.2.23 [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. Example: Input: head = 1->4->3-

[LeetCode] 86. Partition List_Medium tag: Linked 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: Input: head = 1->4->3-