【Leetcode】Insertion Sort List
2015-01-02 20:56
381 查看
Sort a linked list using insertion sort.
简单啦~~~都没debug直接来~~
简单啦~~~都没debug直接来~~
package testAndfun; public class sortedLinkedList { class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public ListNode insertionSortList(ListNode head) { if(head==null || head.next == null) return head; ListNode newHead = new ListNode(head.val); ListNode pointer = head.next; while(pointer!=null){ ListNode innerPointer = newHead; ListNode next = pointer.next; if(pointer.val<=newHead.val){ ListNode oldHead = newHead; newHead = pointer; newHead.next = oldHead; } else{ while(innerPointer.next!=null){ if(pointer.val>innerPointer.val && pointer.val <= innerPointer.next.val){ pointer.next = innerPointer.next; innerPointer.next = pointer; } innerPointer = innerPointer.next; } if(innerPointer.next==null && innerPointer.val<pointer.val){ innerPointer.next = pointer; pointer.next = null; } } pointer = next; } return newHead; } }
相关文章推荐
- LeetCode--insertion-sort-list
- 【leetcode】147 Insertion Sort List
- LeetCode Insertion Sort List
- LeetCode Insertion Sort List
- leetcode_num179_Insertion Sort list
- 【LeetCode with Python】 Insertion Sort List
- Leetcode-insertion-sort-list ***
- [LeetCode]Insertion Sort List
- [LeetCode]Insertion Sort List
- leetcode-Insertion Sort List-147
- LeetCode ||Insertion Sort List
- leetcode 147 Insertion Sort List java 算法
- LeetCode--Insertion Sort List
- LeetCode(147) Insertion Sort List
- leetcode-147 Insertion Sort List
- leetcode---Insertion Sort List
- leetcode之Insertion Sort List
- Insertion Sort List--LeetCode
- leetcode上的Sort a linked list using insertion sort
- Insertion Sort List - LeetCode 147