10.4:给定一个单链表的头节点head,给定一个整数n,将链表按n划分成左边<n、中间==n、右边>n

10.4:给定一个单链表的头节点head,给定一个整数n,将链表按n划分成左边<n、中间==n、右边>n

链表的题目 不要用递归 不要用递归 不要用递归。

package algorithmbasic.class10;

// 给定一个单链表的头节点head,给定一个整数n,将链表按n划分成左边<n、中间==n、右边>n

import java.util.ArrayList;
import java.util.Comparator;

public class SmallerEqualBigger {
    // 内部类
    public static class Node {
        public int value;
        public Node next;

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

    // 方法一
    public static Node listPartition1(Node head, int pivot) {
        Node cur = head;
        Node SH = null;
        Node ST = null;
        Node EH = null;
        Node ET = null;
        Node BH = null;
        Node BT = null;
        while (cur != null) {
            Node next = cur.next;
            cur.next = null;
            if (cur.value < pivot) {
                if (ST == null) {
                    SH = ST = cur;
                } else {
                    ST.next = cur;
                    ST = cur;
                }
            }

            if (cur.value == pivot) {
                if (ET == null) {
                    EH = ET = cur;
                } else {
                    ET.next = cur;
                    ET = cur;
                }
            }

            if (cur.value > pivot) {
                if (BT == null) {
                    BH = BT = cur;
                } else {
                    BT.next = cur;
                    BT = cur;
                }
            }

            cur = next;
        }
        // 三个区域的连接
        ST.next = EH;
        ET.next = BH;

        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);
    }

    // 方法二:
    public static Node listPartition2(Node head, int pivot) {
        if(head == null) {
            return null;
        }
        Node cur = head;
        ArrayList<Node> list = new ArrayList<>();
        int count = 0;
        while (cur != null) {
            list.add(cur);
            count++;
            Node next = cur.next;
            cur.next = null;
            cur = next;
        }
        list.sort(new myComparator());
        head = list.get(0);
        for (int i = 1; i < count; i++) {
            list.get(i - 1).next = list.get(i);
        }
        return head;
    }
}

比较器

    // 比较器
    public static class myComparator implements Comparator<Node> {
        @Override
        public int compare(Node o1, Node o2) {
            return o1.value - o2.value;
        }
    }

    public static void printArray(Node head) {
        if(head == null) {
            return;
        }
        Node cur = head;
        while(cur != null) {
            System.out.print(cur.value + " --> ");
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(10);
        Node node3 = new Node(3);
        Node node4 = new Node(2);
        Node node5 = new Node(10);
        Node node6 = new Node(6);
        Node node7 = new Node(1);
        Node node8 = new Node(3);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        node6.next = node7;
        node7.next = node8;
        listPartition2(node1 , 3);
        printArray(node1);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chen森柏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值