insertion Sort List (链表的插入排序) leecode java
生活随笔
收集整理的這篇文章主要介紹了
insertion Sort List (链表的插入排序) leecode java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
邏輯簡單,代碼難寫,基礎不勞,leecode寫注釋不能出現中文,太麻煩,我寫了大量注釋,鏈表問題最重要的就是你那個指針式干啥的
提交地址https://oj.leetcode.com/problems/insertion-sort-list/
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/ public class Solution {public ListNode insertionSortList(ListNode head) {if(head==null) return head;ListNode h=new ListNode(-1000000); //加入頭結點方面,特別適合頭結點不斷改變的情況,c++要釋放掉,java直接返回頭的next就可h.next=head;ListNode cur=head.next; //cur 保存當前處理的節點head.next=null; //別忘了,新鏈表的尾巴值為空while(cur!=null){ListNode nextNode=cur.next; //保存下一個要處理的點,因為cur會被插入的新鏈表中,所以保存,然后賦給cur(最后一句)ListNode l=h.next;ListNode pre=h;while(l!=null) //找到合適的插入位置,找pre地址{if(l.val<=cur.val){pre=l;l=l.next;}else{break;}}//insert into the listcur.next=pre.next;pre.next=cur;cur=nextNode;}return h.next;}}?
轉載于:https://www.cnblogs.com/hansongjiang/p/3814998.html
總結
以上是生活随笔為你收集整理的insertion Sort List (链表的插入排序) leecode java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jmgraph前端画图组件(html5版
- 下一篇: C语言的本质(4)——浮点数的本质与运算