您的位置:首页 > 其它

【LeetCode】Insertion Sort List

2014-07-09 14:29 295 查看
题目

Sort a linked list using insertion sort.

解答

链表无法像数组那样从后往前依次比较插入,只能从前往后;在链表首部添加一个哨兵可以稍微简化下代码,代码如下:

/**
* 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||head.next==null){
return head;
}
ListNode list=new ListNode(-1);
ListNode pre=list;
ListNode cur=head;
while(cur!=null){
ListNode temp=cur.next; //先用临时的temp将cur的下一节点存起来
pre=list; //将已排的有序链表list赋给pre
while(pre.next!=null&&pre.next.val<cur.val){ //比要比较的值小的,指针指向下一节点
pre=pre.next;
}
cur.next=pre.next; //将要比较的节点和有序的后半部分连接起来
pre.next=cur; //将前半部分和后半部分连接起来
cur=temp; //相当于指针移到下一节点
}
return list.next;
}
}

---EOF---
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: