将单向链表按某值划分成左边小、中间相等、右边大的形式

题目

给定一个单向链表的头节点head,节点的值类型是整型,再给定一个 整 数pivot。实现一个调整链表的函数,将链表调整为左部分都是值小于 pivot 的节点,中间部分都是值等于pivot的节点,右部分都是值大于 pivot的节点。 除这个要求外,对调整后的节点顺序没有更多的要求。 例如:链表9->0->4->5- >1,pivot=3。 调整后链表可以是1->0->4->9->5,也可以是0->1->9->5->4。总 之,满 足左部分都是小于3的节点,中间部分都是等于3的节点(本例中这个部 分为空),右部分都是大于3的节点即可。对某部分内部的节点顺序不做要求。

思路

先将链表转化为数组,采用荷兰国旗解法对数组按要求排序,然后再转回链表;

代码实现

 public static class ListNode {
        public int value;
        public ListNode next;

        public ListNode(int value) {
            this.value = value;
        }
    }

    public static ListNode listPartition(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        ListNode cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        int index = 0;
        ListNode[] arr = new ListNode[size];
        while (head != null) {
            arr[index++] = head;
            head = head.next;
        }
        //采用荷兰国旗解法
        arrPartition(arr, k);
        for (int i = 1; i < arr.length; i++) {
            arr[i - 1].next = arr[i];
        }
        arr[index-1].next = null;
        return arr[0];
    }

    private static void arrPartition(ListNode[] arr, int k) {
        int less = -1;
        int more = arr.length;
        int index = 0;
        while (index != more) {
            if (arr[index].value < k) {
                swap(arr, ++less, index++);
            } else if (arr[index].value == k) {
                index++;
            } else {
                swap(arr, --more, index);
            }
        }
    }

    private static void swap(ListNode[] arr, int i, int j) {
        ListNode temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

  • 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、付费专栏及课程。

余额充值