• [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->5->2 and x = 3,
    return 1->2->2->4->3->5.

    Subscribe to see which companies asked this question

     
    解法1:从头往后,找出第一个值大于等于x的节点,然后在这个节点后面找出第一个值小于x的节点,最后将这个小的节点插在大的节点的前面;然后指针移动到上一步的第一个大节点处,继续循环进行上一步处理,直至后面没有值小于x的节点。
    /**
     * 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 *help = new ListNode(0);
            help->next = head;
            ListNode* prev = help, *curr = prev->next;
            while (curr != NULL) {
                while (curr != NULL && curr->val < x)  { // 找出第一个大于等于x的节点
                    curr = curr->next;
                    prev = prev->next;
                }
                if (curr == NULL) break;
                ListNode *next = curr->next, *temp = curr; // 注意不是从第一个节点开始
                while (next != NULL && next->val >= x) { // 找出第一个小于x的节点
                    next = next->next;
                    temp = temp->next;
                }
                if (next == NULL) break;
                ListNode* tmp = next->next;
                next->next = prev->next; // 将小于x的节点插在大于等于x的节点之前
                prev->next = next;
                temp->next = tmp;
                prev = next; // 前移到下一个节点
                curr = prev->next;
            }
            return help->next;
        }
    };

    实际上外层while循环里的第一个while循环可以拿到外层while循环外面,因为后面每次找到的第一个值大于等于x的节点就是curr本身了。

    /**
     * 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 *help = new ListNode(0);
            help->next = head;
            ListNode* prev = help;
            while (prev->next != NULL && prev->next->val < x) prev = prev->next;
            ListNode* curr = prev;
            while (curr->next != NULL) {
                if (curr->next != NULL && curr->next->val >= x)
                    curr = curr->next;
                else {
                    ListNode* tmp = curr->next;
                    curr->next = tmp->next;
                    tmp->next = prev->next;
                    prev->next = tmp;
                    prev = prev->next;
                }
            }
            return help->next;
        }
    };

    解法2:另一种方法就是遍历一遍链表,分别将值小于x和值大于等于x的节点分为两个链表链接起来,然后再将后者链接在前者的末尾就可以了。

    /**
     * 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) {
            ListNode* head1 = new ListNode(0);
            ListNode* head2 = new ListNode(0);
            ListNode *next1 = head1, *next2 = head2, *curr = head;
            while (curr != NULL) {
                if (curr->val < x) {
                    next1->next = curr;
                    next1 = next1->next;
                    curr = curr->next;
                    next1->next = NULL;
                }
                else {
                    next2->next = curr;
                    next2 = next2->next;
                    curr = curr->next;
                    next2->next = NULL;
                }
            }
            next1->next = head2->next;
            return head1->next;
        }
    };
  • 相关阅读:
    [CareerCup] 1.4 Replace Spaces 替换空格
    [CareerCup] 1.3 Permutation String 字符串的排列
    Android @1x,@2x,@3x 资源文件自动分包工具
    Android 使用xml实现边框阴影,背景渐变效果(附有RGB颜色查询对照表)
    Android中快速实现自定义字体!
    Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.
    Android getRunningTasks和getRunningAppProcesses失效
    Android 实现朋友圈有图片和视频
    Android 仿微信朋友圈拍小视频上传到服务器
    Android setUserVisibleHint-- fragment真正的onResume和onPause方法
  • 原文地址:https://www.cnblogs.com/aprilcheny/p/4969862.html
Copyright © 2020-2023  润新知