【高频】单向链表按某值分成左边小,中间相等,左边大的形式

题目描述

给定一个单项链表的头结点head,节点的值类型是整型,再给定一个整数pivot。实现一个调整链表的函数,将链表的左边都是值小于pivot的节点,右边都是值大于pivot的节点。

例如:

9-->0-->4-->5-->1  pivot = 3

输出:

1-->0-->4-->9-->5 也可以是
0-->1-->9-->5-->4

思路

此题解法还是使用【快排/芬兰国旗】解决问题,区别在于这是链表,要先将链表转换成数组。

   public static class Node {
        int val;
        Node next;

        public Node(int data) {
            this.val = data;
        }
    }

    public static Node ListPartition1(Node head, int pivot) {
        if (head == null) {
            return null;
        }

        Node cur = head;
        // 链表转换成数组
        int i = 0;
        while (cur != null) {
            i++;
            cur = cur.next;
        }
        Node[] nodeArr = new Node[i];
        i = 0;
        cur = head;
        for (i = 0; i < nodeArr.length; i++) {
            nodeArr[i] = cur;
            cur = cur.next;
        }
        
        // 数组的调整
        arrPartition(nodeArr, pivot);
        
        // 数组转换成链表
        for (i = 1; i != nodeArr.length; i++) {
            nodeArr[i - 1].next = nodeArr[i];
        }
        nodeArr[i - 1].next = null;
        
        //返回头结点
        return nodeArr[0];
    }

    // 芬兰国旗问题
    private static void arrPartition(Node[] nodeArr, int pivot) {
        int small = -1;
        int big = nodeArr.length;
        int index = 0;
        while (index != big) {
            if (nodeArr[index].val < pivot) {
                swap(nodeArr, ++small, index++);
            } else if (nodeArr[index].val == pivot) {
                index++;
            } else {
                swap(nodeArr, --big, index);
            }
        }
    }

    private static void swap(Node[] nodeArr, int i, int index) {
        Node temp = nodeArr[i];
        nodeArr[i] = nodeArr[index];
        nodeArr[index] = temp;
    }

进阶

新增要求

  1. 左中右三个部分也要进行排序
  2. 时间复杂度为O(N),空间复杂度为O(1)

显然上述的解决方案不是稳定的,空间复杂度为O(N)

思路:
左中右三个部分要求排序,可以将数值分为三个部分。小的部分,等于的部分,大于的部分。

从头到位遍历链表,对每个链表和pivot进行比较,小的放在小的部分,大的放在大的部分。那么如何存放?

需要每个部分维护两个节点,头结点和尾结点。

  • 插入为空的时候,头结点尾结点都是插入的节点
  • 头结点不为空的时候,尾结点的next指向插入节点,再移动尾结点到最后一个节点。

如此三个部分的排序就是稳定的了。最后一步时进行三个部分的合并,最后就得到排序后的链表。
代码实现如下:

public static Node listPartition2(Node head, int pivot) {
        Node sH = null;
        Node sT = null;
        Node eH = null;
        Node eT = null;
        Node bH = null;
        Node bT = null;
        Node next = null;

        while (head != null) {
            next = head.next;
            head.next = null;
            if (head.val < pivot) {
                if (sH == null) {
                    sH = head;
                    sT = head;
                } else {
                    sT.next = head;
                    sT = head;
                }
            } else if (head.val == pivot) {
                if (eH == null) {
                    eH = head;
                    eT = head;
                } else {
                    eT.next = head;
                    eT = head;
                }
            } else {
                if (bH == null) {
                    bH = head;
                    bT = head;
                } else {
                    bT.next = head;
                    bT = head;
                }
            }
            head = next;
        }

        if (sT != null) {
            sT.next = eH;
            eT = eT == null ? sT : eT;
        }
        if (eT != null) {
            eT.next = bH;
        }
        return sH != null ? sH : eH != null ? eH : bH;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是建立单向链表并按整数从大到小排序的 C++ 代码示例: ```c++ #include <iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode* insertNode(ListNode* head, int val) { ListNode* newNode = new ListNode(val); if (head == NULL) { return newNode; } if (val > head->val) { newNode->next = head; return newNode; } ListNode* cur = head; while (cur->next != NULL && cur->next->val > val) { cur = cur->next; } newNode->next = cur->next; cur->next = newNode; return head; } ListNode* sortList(ListNode* head) { ListNode* newHead = NULL; ListNode* cur = head; while (cur != NULL) { ListNode* next = cur->next; newHead = insertNode(newHead, cur->val); cur = next; } return newHead; } void printList(ListNode* head) { while (head != NULL) { cout << head->val << " "; head = head->next; } cout << endl; } int main() { ListNode* head = NULL; head = insertNode(head, 3); head = insertNode(head, 1); head = insertNode(head, 5); head = insertNode(head, 2); printList(head); head = sortList(head); printList(head); return 0; } ``` 在此示例中,我们定义了一个 `ListNode` 结构体来表示链表节点,包含一个整数 `val` 和一个指向下一个节点的指针 `next`。`insertNode` 函数用于将一个节点插入到链表中,按整数从大到小排序。`sortList` 函数遍历原链表,将每个节点插入到新链表中,最终返回新链表的头节点。`printList` 函数用于打印链表中的所有节点。 在 `main` 函数中,我们创建一个空链表,然后依次插入节点 3、1、5、2,打印原链表并排序后再次打印链表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值