Insertion Sort List
2014-01-27 07:04
501 查看
Insertion Sort List
Total Accepted: 4614 TotalSubmissions: 18226My Submissions
Sort a linked list using insertion sort.
// iterator
public static ListNode insertionSortList(ListNode head){
if(head == null) return null;
ListNode temp = head;
while(temp != null){
ListNode node = temp.next;
while(node!=null){
if(node.val < temp.val){
int t = node.val;
node.val = temp.val;
temp.val = t;
}
node = node.next;
}
temp = temp.next;
}
return head;
}
// recursive
public static ListNode insertionSortList2(ListNode head){
if(head == null) return null;
ListNode temp = head.next;
while(temp!=null){
if(temp.val < head.val){
int t = temp.val;
temp.val = head.val;
head.val = t;
}
temp = temp.next;
}
insertionSortList2(head.next);
return head;
}
相关文章推荐
- LeetCode | Insertion Sort List
- LeetCode Insertion Sort List 链表的插入排序
- Insertion Sort List
- Insertion Sort List
- 【LEETCODE】147- Insertion Sort List [Python]
- Insertion Sort List -- leetcode
- [LeetCode]147 Insertion Sort List
- Insertion Sort List
- Leetcode Insertion Sort List
- leetcode--Insertion Sort List
- Insertion Sort List
- Leetcode#147 Insertion Sort List
- LeetCode: Insertion Sort List(外加对单链表类题目的思考总结)
- leetcode - Insertion Sort List
- LeetCode:Insertion Sort List
- Insertion Sort List
- Insertion Sort List
- Leetcode: Insertion Sort List
- LeetCode147 Insertion Sort List
- #173 Insertion Sort List