日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[Leetcode]147. Insertion Sort List

發(fā)布時間:2025/3/18 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode]147. Insertion Sort List 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Sort a linked list using insertion sort.

鏈表的插入排序

?

思路,遞歸到鏈表尾,然后循環(huán)插入;

?

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 class Solution { 10 public ListNode insertionSortList(ListNode head) { 11 if (head==null||head.next==null) 12 return head; 13 head.next = insertionSortList(head.next); 14 ListNode p = head; 15 while (p.next!=null){ 16 if (p.val>p.next.val){ 17 int tmp = p.val; 18 p.val = p.next.val; 19 p.next.val = tmp; 20 } 21 p = p.next; 22 } 23 return head; 24 } 25 }

?

轉載于:https://www.cnblogs.com/David-Lin/p/7776370.html

總結

以上是生活随笔為你收集整理的[Leetcode]147. Insertion Sort List的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。