LeetCode -- InsertionSor List

来源:互联网 发布:软件企业经营范围 编辑:程序博客网 时间:2024/05/16 01:16

Sort a linked list using insertion sort.

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

很简单的插入,当复习一下咯~

public class Solution {    public ListNode insertionSortList(ListNode head) {        ListNode index = head;        ListNode resulthead = new ListNode(-65535);        while(index != null){        ListNode temp = resulthead;        while(temp.next!=null && temp.next.val < index.val) temp = temp.next;        ListNode newNode = new ListNode(index.val);        newNode.next = temp.next;        temp.next = newNode;        index = index.next;        }        return resulthead.next;    }};


0 0