• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java数据结构链表分割和链表排序使用极快排序、归并排序、集合排序、迭代、递归,刷题的重点

武飞扬头像
xyk:
帮助1

本篇主要介绍在单链表进行分割,单链表进行分隔并使用快速排序、归并排序、集合排序、迭代、递归等方法的总结,愿各位大佬喜欢~~

86. 分隔链表 - 力扣(LeetCode)

148. 排序链表 - 力扣(LeetCode)


目录

一、分隔链表

二、排序链表

2.1先介绍一个最容易最简单的方法

2.2普通归并排序(自顶向下)

2.3借鉴大佬的归并排序(自底向上也是最难的,空间复杂度o(1))

2.4面试官让你用快排实现,不会做也得会

2.5快排2:


一、分隔链表

86. 分隔链表 - 力扣(LeetCode)

给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。

在分隔链表时,先考虑如何去分隔链表,怎么保证节点的next不形成环。

学新通

方法一:也是笨方法,找到第一个>x的值将小的值都逐个进行头插,随后将next指向第一个>x的值

  1.  
    class Solution {
  2.  
    public ListNode partition(ListNode head, int x) {
  3.  
    if (head == null || head.next == null) {
  4.  
    return head;
  5.  
    }
  6.  
    ListNode mark = new ListNode(0, head);
  7.  
    ListNode cur1 = mark;
  8.  
    while (cur1.next != null && cur1.next.val < x) {
  9.  
    cur1 = cur1.next;
  10.  
    }
  11.  
    ListNode firstMaxOrNull = cur1.next;
  12.  
    ListNode cur2 = cur1;
  13.  
    while (cur2 != null && cur2.next != null) {
  14.  
    if (cur2.next.val >= x) {
  15.  
    cur2 = cur2.next;
  16.  
    } else {
  17.  
    cur1.next = cur2.next;
  18.  
    cur2.next = cur2.next.next;
  19.  
    cur1 = cur1.next;
  20.  
    cur1.next = null;
  21.  
    }
  22.  
    }
  23.  
    cur1.next = firstMaxOrNull;
  24.  
    return mark.next;
  25.  
    }
  26.  
    }
学新通

方法二:创建俩个链表进行分别插入,最后合并俩个链表

  1.  
    public ListNode partition(ListNode head, int x) {
  2.  
    ListNode minLink = new ListNode(0);//记录小值链表的头
  3.  
    ListNode minP = minLink;//对小表操作用的指针
  4.  
     
  5.  
    ListNode maxLink = new ListNode(0);//记录大值链表的头
  6.  
    ListNode maxP = maxLink;//同理
  7.  
     
  8.  
    while(head!=null){
  9.  
    if(head.val < x){//找到小的值
  10.  
    minP.next = head;//放入minLink中,操作指针后移一位
  11.  
    minP = head;
  12.  
    }else{
  13.  
    maxP.next = head;//放入maxLink中,操作指针后移一位
  14.  
    maxP = head;
  15.  
    }
  16.  
    head = head.next;
  17.  
    }
  18.  
    //遍历完成后记得后一段链表的最后节点指向null;
  19.  
    maxP.next = null;
  20.  
    //两段拼接
  21.  
    minP.next = maxLink.next;
  22.  
     
  23.  
    return minLink.next;
  24.  
    }
学新通

二、排序链表

148. 排序链表 - 力扣(LeetCode)

给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。

上一道题并没有让合并之后还要进行排序,本题不仅要分隔之后还要进行排序,此时可以想到很多种方法,比如堆排序,快速排序,归并排序等等。看似简单,却是很多大厂的面试题。

题目的进阶问题要求达到 O(nlogn) 的时间复杂度和 O(1) 的空间复杂度

时间复杂度是 O(nlogn) 的排序算法包括归并排序、堆排序和快速排序(快速排序的最差时间复杂度是 O(n^2),其中最适合链表的排序算法是归并排序。

学新通

2.1先介绍一个最容易最简单的方法

都存到集合中,排序后再进行赋值,如果写出这样,面试可能要回家喽!!!

  1.  
    class Solution {
  2.  
    //思路,先把链表存到数组,排序后重新更新链表
  3.  
    public ListNode sortList(ListNode head) {
  4.  
    ArrayList<Integer> list = new ArrayList();
  5.  
    ListNode p = head;
  6.  
    while(p != null){
  7.  
    list.add(p.val);
  8.  
    p = p.next;
  9.  
    }
  10.  
    Collections.sort(list);
  11.  
    p = head;
  12.  
    for(int i = 0;i<list.size();i ){
  13.  
    p.val = list.get(i);
  14.  
    p = p.next;
  15.  
    }
  16.  
    return head;
  17.  
    }
  18.  
    }
学新通

2.2普通归并排序(自顶向下)

使用快慢指针进行找到中间节点,然后进行归并排序

  1.  
    public ListNode sortList(ListNode head){
  2.  
    return mergeSort(head);
  3.  
    }
  4.  
     
  5.  
    // 找到一个链表的中点
  6.  
    public ListNode findMid(ListNode head) {
  7.  
    if (head == null) return head;
  8.  
     
  9.  
    ListNode fast = head.next; // 快指针 每次走2步
  10.  
    ListNode slow = head; // 慢指针 每次走1步
  11.  
    while (fast != null && fast.next != null) {
  12.  
    fast = fast.next.next;
  13.  
    slow = slow.next;
  14.  
    }
  15.  
    return slow;
  16.  
    }
  17.  
     
  18.  
    public ListNode mergeSort(ListNode head) {
  19.  
    // 当head.next==null时 说明当前链表只有一个元素 无序再排序
  20.  
    if (head == null || head.next == null) {
  21.  
    return head;
  22.  
    }
  23.  
    // 找到中间节点
  24.  
    ListNode mid = findMid(head);
  25.  
    // 存储中间节点的下一个结点
  26.  
    ListNode next = mid.next;
  27.  
    // 从中间结点断开 分别对两边进行mergeSort
  28.  
    mid.next = null;
  29.  
    // 返回排序后的头节点
  30.  
    ListNode left = mergeSort(head);
  31.  
    ListNode right = mergeSort(next);
  32.  
     
  33.  
    // 返回合并之后的头节点
  34.  
    return merge(left, right);
  35.  
    }
  36.  
     
  37.  
    public ListNode merge(ListNode l1, ListNode l2) {
  38.  
    ListNode dummy = new ListNode(-1);
  39.  
    ListNode curr = dummy;
  40.  
    while (l1 != null && l2 != null) {
  41.  
    if (l1.val < l2.val) {
  42.  
    curr.next = l1;
  43.  
    l1 = l1.next;
  44.  
    } else {
  45.  
    curr.next = l2;
  46.  
    l2 = l2.next;
  47.  
    }
  48.  
    curr = curr.next;
  49.  
    }
  50.  
     
  51.  
    if (l1 != null) {
  52.  
    curr.next = l1;
  53.  
    }
  54.  
     
  55.  
    if (l2 != null) {
  56.  
    curr.next = l2;
  57.  
    }
  58.  
    return dummy.next;
  59.  
    }
学新通

2.3借鉴大佬的归并排序(自底向上也是最难的,空间复杂度o(1))

  1.  
    class Solution {
  2.  
    public ListNode sortList(ListNode head) {
  3.  
    ListNode dummy = new ListNode(-1, head);
  4.  
    // 获取链表的长度
  5.  
    int len = 0;
  6.  
    ListNode curr = head;
  7.  
    while (curr!=null) {
  8.  
    len ;
  9.  
    curr = curr.next;
  10.  
    }
  11.  
     
  12.  
    // 循环遍历
  13.  
    // 外层遍历step 内层处理每step个元素进行一次merge
  14.  
    for (int step=1; step<len; step*=2) {
  15.  
    ListNode tail = dummy;
  16.  
    curr = dummy.next;
  17.  
    while (curr!=null) {
  18.  
    ListNode left = curr;
  19.  
    ListNode right = cut(left, step);
  20.  
    curr = cut(right, step);
  21.  
    tail.next = merge(left, right);
  22.  
    while (tail.next!=null) {
  23.  
    tail = tail.next;
  24.  
    }
  25.  
    }
  26.  
    }
  27.  
    return dummy.next;
  28.  
    }
  29.  
     
  30.  
    // 将链表从from开始切掉前step个元素,返回后一个元素
  31.  
    public ListNode cut(ListNode from, int step) {
  32.  
    step--;
  33.  
    while (from!=null && step>0) {
  34.  
    from = from.next;
  35.  
    step--;
  36.  
    }
  37.  
    if (from==null) {
  38.  
    return null;
  39.  
    } else {
  40.  
    ListNode next = from.next;
  41.  
    from.next = null;
  42.  
    return next;
  43.  
    }
  44.  
    }
  45.  
     
  46.  
    // 将两个有序链表合并成一个,返回合并后的头节点
  47.  
    public ListNode merge(ListNode l1, ListNode l2) {
  48.  
    ListNode dummy = new ListNode();
  49.  
    ListNode curr = dummy;
  50.  
    while (l1!=null && l2!=null) {
  51.  
    if (l1.val<l2.val) {
  52.  
    curr.next = l1;
  53.  
    l1 = l1.next;
  54.  
    } else {
  55.  
    curr.next = l2;
  56.  
    l2 = l2.next;
  57.  
    }
  58.  
    curr = curr.next;
  59.  
    }
  60.  
    if (l1!=null) {
  61.  
    curr.next = l1;
  62.  
    }
  63.  
    if (l2!=null) {
  64.  
    curr.next = l2;
  65.  
    }
  66.  
    return dummy.next;
  67.  
    }
  68.  
    }
学新通

2.4面试官让你用快排实现,不会做也得会

  1.  
    public ListNode sortList3(ListNode head) {
  2.  
    return quickSortLinkedList(head)[0];
  3.  
    }
  4.  
     
  5.  
    public ListNode[] quickSortLinkedList(ListNode head) {
  6.  
    if(head == null || head.next == null) return new ListNode[]{head,head};
  7.  
    //pivot为head,定义跟踪分割左右两个链表的头尾指针
  8.  
    ListNode p = head.next,headSmall= new ListNode(),
  9.  
    headBig = new ListNode(),tailSmall = headSmall, tailBig = headBig;
  10.  
     
  11.  
    //partition操作,以pivot为枢纽分割为两个链表
  12.  
    while(p != null){
  13.  
    if(p.val < head.val){
  14.  
    tailSmall.next = p;
  15.  
    tailSmall = tailSmall.next;
  16.  
    }else{
  17.  
    tailBig.next = p;
  18.  
    tailBig = tailBig.next;
  19.  
    }
  20.  
    p = p.next;
  21.  
    }
  22.  
     
  23.  
    //断开<pivot的排序链表、pivot、>=pivot的排序链表,链表变为三个部分
  24.  
    head.next = null;
  25.  
    tailSmall.next = null;
  26.  
    tailBig.next = null;
  27.  
     
  28.  
    //递归partition
  29.  
    ListNode[] left = quickSortLinkedList(headSmall.next);
  30.  
    ListNode[] right = quickSortLinkedList(headBig.next);
  31.  
     
  32.  
     
  33.  
    //如果有<pivot的排序链表、连接pivot
  34.  
    if(left[1] != null) {
  35.  
    left[1].next = head;
  36.  
    }
  37.  
    //连接pivot、>=pivot的排序链表
  38.  
    head.next = right[0];
  39.  
     
  40.  
    //确定排序后的头节点和尾节点
  41.  
    ListNode newHead,newTail;
  42.  
    if(left[0] != null) newHead = left[0];
  43.  
    else newHead = head;
  44.  
    if(right[1] != null) newTail = right[1];
  45.  
    else newTail = head;
  46.  
     
  47.  
    //返回当前层递归排序好的链表头节点和尾节点
  48.  
    return new ListNode[]{newHead,newTail};
  49.  
    }
学新通

2.5快排2:

用6个指针,分别指向小于部分的头h1和尾t1、等于部分的头h2和尾t2、大于部分的头h3和尾t3

  1.  
    class Solution {
  2.  
    public ListNode sortList(ListNode head) {
  3.  
    return quickSort(head);
  4.  
    }
  5.  
     
  6.  
    public ListNode quickSort(ListNode head) {
  7.  
    if (head==null || head.next==null) {
  8.  
    return head;
  9.  
    }
  10.  
    ListNode h1 = new ListNode();
  11.  
    ListNode h2 = new ListNode();
  12.  
    ListNode h3 = new ListNode();
  13.  
    ListNode t1 = h1;
  14.  
    ListNode t2 = h2;
  15.  
    ListNode t3 = h3;
  16.  
    ListNode curr = head;
  17.  
    int pivot = getMid(head).val; // 用中间节点的原因是,如果每次用最左边的结点,对于纯递增和纯递减的case就退化为O(n)
  18.  
     
  19.  
    while (curr!=null) {
  20.  
    ListNode next = curr.next;
  21.  
    if (curr.val < pivot) {
  22.  
    curr.next = null;
  23.  
    t1.next = curr;
  24.  
    t1 = t1.next;
  25.  
    } else if (curr.val == pivot) {
  26.  
    curr.next = null;
  27.  
    t2.next = curr;
  28.  
    t2 = t2.next;
  29.  
    } else {
  30.  
    curr.next = null;
  31.  
    t3.next = curr;
  32.  
    t3 = t3.next;
  33.  
    }
  34.  
    curr = next;
  35.  
    }
  36.  
     
  37.  
    // < 的链表和 > 的链表分别快排
  38.  
    h1 = quickSort(h1.next);
  39.  
    h3 = quickSort(h3.next);
  40.  
     
  41.  
    // h1链表不一定有元素 h2链表一定有元素 先把h2、h3连起来
  42.  
    h2 = h2.next;
  43.  
    t2.next = h3;
  44.  
    // 如果h1链表为空 直接返回h2即可
  45.  
    // 否则找到h1链表的结尾,连上h2的头
  46.  
    if (h1==null) {
  47.  
    return h2;
  48.  
    } else {
  49.  
    t1 = h1;
  50.  
    // 找到t1链表的结尾
  51.  
    while (t1.next!=null) {
  52.  
    t1 = t1.next;
  53.  
    }
  54.  
    t1.next = h2;
  55.  
    return h1;
  56.  
    }
  57.  
    }
  58.  
     
  59.  
    public ListNode getMid(ListNode head) {
  60.  
    ListNode fast = head;
  61.  
    ListNode slow = head;
  62.  
    while (fast!=null && fast.next!=null) {
  63.  
    fast = fast.next.next;
  64.  
    slow = slow.next;
  65.  
    }
  66.  
    return slow;
  67.  
    }
  68.  
     
  69.  
    }
学新通

merge 函数里只有一个 dummy 指针是新建的,后续节点都是直接拼接原链表的数据,所以一次 merge 操作的空间复杂度是 O(1)。而且 CPU 一次只能执行一个 merge 函数,merge 函数执行一次后,栈上申请的空间都释放掉了,所以,没有递归的解法,总的空间复杂度还是 O(1)。 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiiiba
系列文章
更多 icon
同类精品
更多 icon
继续加载