Sorted circular linked list solution

sorted circular linked list

topic description

Given a point in a cyclic monotonically non-decreasing list , write a function to insert a new element insertVal into the list so that the list is still in ascending cyclic order.
The given can be a pointer to any vertex in this list, not necessarily the pointer to the smallest element in this list.
If there are multiple insertion positions that meet the conditions, you can choose any position to insert a new value, and the entire list remains in order after insertion.
If the list is empty (the given node is null), you need to create a circular sorted list and return this node. otherwise. Please return the original given node.

Source: LeetCode
Link: https://leetcode-cn.com/problems/4ueAj6
insert image description here
insert image description here

analyze

  1. If the head node is empty, create a new node directly, point nextto itself, and return head;
  2. When the head is not empty, it can be divided into three cases:
    - If the value of the next node found is greater than or equal to insertVal, and the value of the current node is less than or equal to insertVal, it is allowed to insert this new node at the current node position (currently an incremental sequence );
    - If the end and beginning of the incrementing interval meet in the middle, it should be judged insertValwhether it is greater than or equal to the current node (that is, the maximum value of the incrementing interval); or whether it is less than or equal to the next node of the current node (that is, decreasing The minimum value of the interval), if it matches, the next of the current node points to insertVal;
    - If all the values ​​are equal, it is necessary to additionally judge nextwhether the node is equal to the head node. If the previous judgment logic cannot handle the situation, it will eventually be leaked here to judge whether Back to the head node.

Replenish

  • Monotonically non-decreasing sequence: not monotonically increasing, because there may be repeated values, for example: [1, 2, 3, 3, 5];
  • Similarly, monotonically non-increasing sequences.

the code

/*
// Definition for a Node.
class Node {
    public int val;
    public Node next;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _next) {
        val = _val;
        next = _next;
    }
};
*/

class Solution {
    
    
    public Node insert(Node head, int insertVal) {
    
    
        Node node = new Node(insertVal);
        if(head == null) {
    
    
            head = node;
            head.next = node;
            return head;
        }
        Node cur = head;
        while(true) {
    
    
            if(cur.next.val >= insertVal && cur.val <= insertVal) {
    
    
                break;
            }
            if(cur.next.val < cur.val) {
    
    
                if(insertVal > cur.val || insertVal < cur.next.val) {
    
    
                    break;
                }
            }
            if(cur.next == head) {
    
    
                break;
            }
            cur = cur.next;
        }
        Node nextNode = cur.next;
        cur.next = node;
        node.next = nextNode;
        return head;
    }
}

Sorted circular linked list solution

Guess you like

Origin blog.csdn.net/weixin_45832482/article/details/122691303