解题思路:

设置一个dummy头结点,然后将原链表中的节点一个个拆下来,按升序拼接到新的链表中。


提交代码:

class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode dummy=new ListNode(-1);
        ListNode cur=dummy;
        while(head!=null) {
        	ListNode pn=head.next;
        	cur=dummy;
        	while(cur.next!=null&&cur.next.val<=head.val) {
        		cur=cur.next;
        	}
        	head.next=cur.next;
        	cur.next=head;
        	head=pn;
        }
    	return dummy.next;
    }
}

运行结果:
【leetcode】147.(Meduim)Insertion Sort List

相关文章:

  • 2022-12-23
  • 2022-01-14
  • 2021-08-01
  • 2021-11-07
猜你喜欢
  • 2021-06-15
  • 2021-08-24
  • 2021-10-18
  • 2022-02-05
  • 2022-01-15
  • 2021-06-01
  • 2021-10-03
相关资源
相似解决方案