[Leetcode]147. Insertion Sort List
生活随笔
收集整理的這篇文章主要介紹了
[Leetcode]147. Insertion Sort List
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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 }?
轉(zhuǎn)載于:https://www.cnblogs.com/David-Lin/p/7776370.html
總結(jié)
以上是生活随笔為你收集整理的[Leetcode]147. Insertion Sort List的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 想服务器不宕机,你了解Linux“体检”
- 下一篇: [Leetcode]160. Inter