您的位置:首页 > 其它

Insertion Sort List

2015-10-01 11:01 169 查看
题目:

Sort a linked list using insertion sort.

分析:

用插入排序法给链表排序,就好像小样送到了狼窝里一样O(∩_∩)O~

参考代码如下:
http://blog.csdn.net/linhuanmars/article/details/21144553
/**
* Definition for singly-linked list.
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode insertionSortList(ListNode head) {
if(head == null)
return null;
ListNode helper = new ListNode(0);
ListNode pre = helper;
ListNode cur = head;
while(cur!=null)
{
ListNode next = cur.next;
pre = helper;
while(pre.next!=null && pre.next.val<=cur.val)
{
pre = pre.next;
}
cur.next = pre.next;
pre.next = cur;
cur = next;
}
return helper.next;
}
}


真是每个人有每个人的断法啊,看不懂o(╯□╰)o,but反正我有我自己的办法(*^__^*) ……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: