首页 > 其他分享 >力扣递归 两道简单题合成一道中等题之148. 排序链表

力扣递归 两道简单题合成一道中等题之148. 排序链表

时间:2024-02-09 19:00:29浏览次数:19  
标签:力扣 head ListNode val 148 next 链表 return null

递归归并排序,先找到终点,再合并两个链表

 

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

 

示例 1:

输入:head = [4,2,1,3]
输出:[1,2,3,4]

示例 2:

输入:head = [-1,5,3,4,0]
输出:[-1,0,3,4,5]

示例 3:

输入:head = []
输出:[]
/**  * Definition for singly-linked list.  * public class ListNode {  * int val;  * ListNode next;  * ListNode() {}  * ListNode(int val) { this.val = val; }  * ListNode(int val, ListNode next) { this.val = val; this.next = next; }  * }  */ class Solution {     public ListNode sortList(ListNode head) {         return mergeSort(head);
    }
    ListNode mergeSort(ListNode head) {         if (head == null || head.next == null) {             return head;         }         ListNode mid = findMid(head);         ListNode listNodeRight = mergeSort(mid.next);         mid.next = null;         ListNode listNodeLeft = mergeSort(head);         return merge(listNodeLeft, listNodeRight);     }
    ListNode findMid(ListNode head) {         if (head == null) {             return head;         }         ListNode slow = head;         ListNode fast = head.next;         while (fast != null && fast.next != null) {             slow = slow.next;             fast = fast.next.next;         }         return slow;     }
    ListNode merge(ListNode listNode1, ListNode listNode2) {         if (listNode1 == null) {             return listNode2;         } else if (listNode2 == null) {             return listNode1;         } else if (listNode1.val < listNode2.val) {             listNode1.next = merge(listNode1.next, listNode2);             return listNode1;         } else {             listNode2.next = merge(listNode1, listNode2.next);             return listNode2;         }
    } }     /**  * Definition for singly-linked list.  * public class ListNode {  * int val;  * ListNode next;  * ListNode() {}  * ListNode(int val) { this.val = val; }  * ListNode(int val, ListNode next) { this.val = val; this.next = next; }  * }  */ class Solution {     public ListNode sortList(ListNode head) {         if (head == null || head.next == null) {             return head;         }         ListNode mid = findMid(head);         ListNode listNodeRight = sortList(mid.next);         mid.next = null;         ListNode listNodeLeft = sortList(head);         return merge(listNodeLeft, listNodeRight);     }
    ListNode findMid(ListNode head) {         if (head == null) {             return head;         }         ListNode slow = head;         ListNode fast = head.next;         while (fast != null && fast.next != null) {             slow = slow.next;             fast = fast.next.next;         }         return slow;     }
    ListNode merge(ListNode listNode1, ListNode listNode2) {         if (listNode1 == null) {             return listNode2;         } else if (listNode2 == null) {             return listNode1;         } else if (listNode1.val < listNode2.val) {             listNode1.next = merge(listNode1.next, listNode2);             return listNode1;         } else {             listNode2.next = merge(listNode1, listNode2.next);             return listNode2;         }
    } }

标签:力扣,head,ListNode,val,148,next,链表,return,null
From: https://www.cnblogs.com/JavaYuYin/p/18012588

相关文章

  • 力扣快慢双指针之876. 链表的中间结点
    给你单链表的头结点head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。 示例1:输入:head=[1,2,3,4,5]输出:[3,4,5]解释:链表只有一个中间结点,值为3。示例2:输入:head=[1,2,3,4,5,6]输出:[4,5,6]解释:该链表有两个中间结点,值分别为3......
  • 力扣递归之88. 合并两个有序数组
    给你两个按非递减顺序排列的整数数组 nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2到nums1中,使合并后的数组同样按非递减顺序排列。注意:最终,合并后数组不应由函数返回,而是存储在数组nums1中。为了应对这种情况,nums1的初......
  • 力扣递归之21. 合并两个有序链表
    将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。  示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,4]示例2:输入:l1=[],l2=[]输出:[]示例3:输入:l1=[],l2=[0]输出:[0]classSolution{publicListN......
  • 力扣刷题——SQL
    力扣刷题——高频SQL50题:题目链接:https://leetcode.cn/problems/department-top-three-salaries/?envType=study-plan-v2&envId=sql-free-50185.部门工资前三高的所有员工:表:Employee:Department+--......
  • CF1483F Exam
    我永远喜欢数据结构神仙\(\color{maroon}*3400\)字符串题。感觉现有的一篇SA题解讲的不太清楚,来一篇更加清楚、严谨的SA题解。洛谷CF给出\(n\)个字符串\(s_1\sims_n\),求有多少对\((i,j)\),满足:\(1\lei,j\len\)。\(s_j\)是\(s_i\)的真子串。不存在\(k\)......
  • 数据结构(一)单链表---以题为例
    实现一个单链表,链表初始为空,支持三种操作:向链表头插入一个数;删除第 k 个插入的数后面的数;在第 k 个插入的数后插入一个数。现在要对该链表进行 M 次操作,进行完所有操作后,从头到尾输出整个链表。注意:题目中第 k 个插入的数并不是指当前链表的第 k 个数。例如操作......
  • [数据结构] 链表
    写在前面菜鸡博主开始复习了,先从数据结构开始吧(其实是每天复习高数太累了)1.单链表单链表是线性表的链式存储,是指通过一组任意的存储单元来存储线性表中的数据元素。对每个链表节点,除了存放元素自身的信息之外,还需要存放一个指向其后继的指针(如下图所示)单链表的节点可以用如......
  • 【C++】力扣101-平方数之和
    给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2+b2=c 。使用双指针:#include<iostream>#include<math.h>usingnamespacestd;booljudge(longc){if(c<0)returnfalse;longa=0;longb=(int)sqrt(c);longsum=0;while......
  • Python数据结构与算法03-单循环链表
    单循环链表classListNode:def__init__(self,val,next=None):self.val=valself.next=nextclassSingleLoopLinkList:def__init__(self,node=None):self.__head=nodeifnode:#如果不是空链表node.next=node......
  • 力扣 34. 在排序数组中查找元素的第一个和最后一个位置
    Problem: 34.在排序数组中查找元素的第一个和最后一个位置思路找到大于等于target的下标,然后遍历之后的数组,找到最后的下标。classSolution{public:intf(vector<int>&nums,inttarget){intl=0,r=nums.size()-1;intmid=floor(l+(r-l)*1.0/2);......