Q147. Insertion Sort List

直达:https://leetcode.com/problems/insertion-sort-list/description/

Sort a linked list using insertion sort.

分析

知识点在于链表的插入,分成两种情况。

  1. 在头结点之前插入

如下图在3之前插入1:

  1. 在中间或者尾部插入,如下图

首先顺序遍历res链表,找到该插入的位置,即将head插入l2和l1之间

C++代码

Last updated

Was this helpful?