- 链表为空时,需要新建一个链表返回。
- 链表只有一个头结点,则新结点插入到头结点前后都可以。
- 查询链表中按照有序来进行分类讨论,curr和next指向第一个结点和第二个结点, 比如链表序列是5 7 1 3
- 我要插入6或2,则只需要判断insertVal >= curr.Val && insertVal <= next.Val
- 我要插入8或者0,则需要判断在curr.Val > next.Val的情况下insertVal > curr.Val || insertVal < next.Val
- 我要插入4,则前面两个条件都不匹配,最后自然会将curr指向最后一个结点,我在循环结束后,将他插入到curr.next上
func insert(head *Node, insertVal int) *Node {
node := &Node{Val: insertVal}
//链表为空
if head == nil {
node.Next = node
return node
}
//链表只有一个结点
if head.Next == head {
head.Next = node
node.Next = head
return head
}
curr, next := head, head.Next
for next != head{
if insertVal >= curr.Val && insertVal <= next.Val{
break
}
if curr.Val > next.Val{
if insertVal > curr.Val || insertVal < next.Val{
break
}
}
curr = curr.Next
next = next.Next
}
curr.Next = node
node.Next = next
return head
}



