链表插入排序

2018/08/13 Offer 共 644 字,约 2 分钟

title

problem link

Sort a linked list using insertion sort.

对链表插入排序

solution


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    void insert(ListNode head,ListNode node){
        ListNode pre=head,p=head.next;
        while (p!=null&&p.val<node.val){
            pre=pre.next;
            p=p.next;
        }
        node.next=p;
        pre.next=node;
    }
    public ListNode insertionSortList(ListNode head) {
        ListNode newhead=new ListNode(Integer.MIN_VALUE);
        newhead.next=head;
        ListNode p=head,pre=newhead;
        while (p!=null){
            if(pre.val>p.val){
                pre.next=p.next;
                p.next=null;
                insert(newhead,p);
                p=pre.next;
                
            }else{
                pre=pre.next;
                p=p.next;
            }

        }
        return newhead.next;
    }
}

Search

    Table of Contents