---
# System prepended metadata

title: 0708. Insert into a Sorted Circular Linked List
tags: [Leetcode, FaceBook, Medium]

---

# 0708. Insert into a Sorted Circular Linked List
###### tags: `Leetcode` `FaceBook` `Medium`
Link: https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list/
## 思路
只要考虑到所有情况就好了~
建议再写一遍 
有点麻烦

注意：因为是个环才需要写成do while的形式，正常情况下 linked list不会回到原来的status
例如：开始是curr = head，结束条件就是curr == null，不会出现因为结束条件需要是curr == head，而跟起始条件冲突的情况
## Code
```java=
class Solution {
    public Node insert(Node head, int insertVal) {
        Node newNode = new Node(insertVal);
        if(head == null) {
            newNode.next = newNode;
            return newNode;
        }
        Node prev = head;
        Node curr = head.next;
        
        do{
            if(curr.val>=insertVal && insertVal>=prev.val){
                prev.next = newNode;
                newNode.next = curr;
                return head;
            }
            else if(prev.val>curr.val){
                if(insertVal<=curr.val||insertVal>=prev.val){
                    prev.next = newNode;
                    newNode.next = curr;
                    return head;
                }
            }
            prev = curr;
            curr = curr.next;
        } while(prev != head);
        
        prev.next = newNode;
        newNode.next = curr;
        
        return head;
    }
}
```