力扣链表排序

链表

链表排序

使用java List

所有节点加入list.使用list排序,然后串起来返回。最后一个节点的next指向null

 public ListNode sortList(ListNode head, int type) {   
   if (head == null || head.next == null) {
        return head;
    }
    ListNode currentNode = head;
    List<ListNode> listNodeList = new ArrayList<>();
    while (currentNode != null) {
        listNodeList.add(currentNode);
        currentNode = currentNode.next;

    }
    listNodeList.sort(new Comparator<ListNode>() {
        @Override
        public int compare(ListNode o1, ListNode o2) {
            return o1.val - o2.val;
        }
    });
    ListNode newHead = listNodeList.get(0);
    currentNode = newHead;
    for (int i = 1; i < listNodeList.size(); i++) {
        currentNode.next = listNodeList.get(i);
        currentNode = currentNode.next;
    }
    currentNode.next = null;
    return newHead;
}

归并排序思想

找到链表的中间节点,然后两边分别排序,最后在进行merge操作

每个小链表都去排序好自己的小链表。

  1. 找到中间节点,排好左边的,拍好右边的。
  2. 分割的原则:只要链表的长度大于1就进行分割。
package com.vitamin.list;

import org.junit.Test;

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

/**
 * 链表排序
 *
 * @author vitamin
 * 2022/4/6 22:34
 */
public class SortList {

    @Test
    public void test() {
        ListNode node1 = new ListNode(4, null);
        ListNode node2 = new ListNode(2, null);
        ListNode node3 = new ListNode(1, null);
        ListNode node4 = new ListNode(3, null);
        ListNode node5 = new ListNode(6, null);
        ListNode node6 = new ListNode(7, null);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        ListNode node = sortList(node1);
        while (node != null) {
            System.out.println(node.val);
            node = node.next;
        }
    }
	
    public ListNode sortList(ListNode head) {

        if (head == null || head.next == null) {
            return head;
        }
        // 得到链表的中间节点
        ListNode middleNode = getMiddleNode(head);
        ListNode next = middleNode.next;
        middleNode.next = null;
        ListNode right = sortList(next);
        ListNode left = sortList(head);
        return merge(left, right);

    }
	/**
	* 创建一个虚拟的头节点,一个当前指针。
	* 返回虚拟头节点的下一个指针
	**/
    private ListNode merge(ListNode left, ListNode right) {
        ListNode resultNode = new ListNode(-1, null);
        ListNode currentNdoe = resultNode;
        while (left != null && right != null) {
            if (left.val < right.val) {
                currentNdoe.next = left;
                left = left.next;
            } else {
                currentNdoe.next = right;
                right = right.next;
            }
            currentNdoe = currentNdoe.next;
        }
        if (left != null) {
            currentNdoe.next = left;
        }
        if (right != null) {
            currentNdoe.next = right;
        }
        return resultNode.next;
    }


    public ListNode getMiddleNode(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode fastNode = head;
        ListNode slowNode = head;
        while (fastNode.next != null && fastNode.next.next != null) {
            fastNode = fastNode.next.next;
            slowNode = slowNode.next;
        }
        return slowNode;
    }

   
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值