码迷,mamicode.com
首页 > 其他好文 > 详细

Insertion Sort List

时间:2016-08-20 10:09:38      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

Sort a linked list using insertion sort.

 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 public class Solution {
10     public ListNode insertionSortList(ListNode head) {
11         ListNode dummy = new ListNode(0);
12         while (head != null) {
13             ListNode node = dummy;
14             while (node.next != null && node.next.val < head.val) {
15                 node = node.next;
16             }
17             ListNode temp = head.next;
18             head.next = node.next;
19             node.next = head;
20             head = temp;
21         }
22         return dummy.next;
23     }
24 }

 

Insertion Sort List

标签:

原文地址:http://www.cnblogs.com/FLAGyuri/p/5789615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!