LintCode-链表插入排序

来源:互联网 发布:电子设备审计软件功能 编辑:程序博客网 时间:2024/05/21 12:45


容易 链表插入排序

用插入排序对链表排序

您在真实的面试中是否遇到过这个题? 
Yes
样例

Given 1->3->2->0->null, return 0->1->2->3->null

冒泡法遍历



/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {    /**     * @param head: The first node of linked list.     * @return: The head of linked list.     */        public ListNode insertionSortList(ListNode head) {boolean flag = true;ListNode frist = head;ListNode second = head;if(head.next == null || head == null){return head;}else{frist = head.next;}while(flag){flag = false;do{if(frist.val<second.val){int x =frist.val;frist.val = second.val;second.val = x;flag = true;}frist = frist.next;second = second.next;}while(frist != null);frist = head.next;second = head;}return head;}}


0 0
原创粉丝点击