/* // Definition for a Node. class Node { public: int val; Node* next; Node() {} Node(int _val) { val = _val; next = NULL; } Node(int _val, Node* _next) { val = _val; next = _next; } }; */
classSolution { public: Node* insert(Node* head, int insertVal){ if (!head) { auto res = new Node(insertVal); res->next = res; return res; } auto prev = head, curr = head->next; do { // 因为上来如果判断prev != head没法写循环,改成dowhile就可以了 if (prev->val <= insertVal && insertVal <= curr->val) break; if (prev->val > curr->val && (prev->val <= insertVal || insertVal <= curr->val)) break; prev = curr; curr = prev->next; } while (prev != head); prev->next = new Node(insertVal, curr); return head; } };