# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up