[leetcode] Insert sort list

来源:互联网 发布:2017年进出口贸易数据 编辑:程序博客网 时间:2024/05/01 21:25

https://leetcode.com/problems/insertion-sort-list/

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution {    public ListNode insertionSortList(ListNode head) {        if (head == null || head.next == null) {return head;}ListNode h = new ListNode(0), p = head, pre = h, q = head.next;h.next = head;p.next = null;while (q != null) {while (p != null && q.val >= p.val) {pre = p;p = p.next;}ListNode t = q.next;pre.next = q;q.next = p;q = t;pre = h;p = h.next;}return h.next;    }}


0 0
原创粉丝点击