力扣每日一题2022-06-18中等题:排序的循环链表

排序的循环链表

    • 题目描述
    • 思路
      • 一次遍历
        • Python实现
        • Java实现
        • C++实现


题目描述

排序的循环链表


思路

一次遍历

如果循环链表为空,则插入一个新节点,并将新节点的next指针指向自身,插入新节点后得到只有一个节点的循环链表,该循环链表是有序的,将插入的新节点作为新的头节点返回。
如果循环链表的头节点的next指针指向自身,则链表中只有一个结点,在头节点后插入新节点,将头节点的next指针指向新节点,新节点的next指针指向头节点,此时链表有两个节点且有序,返回头节点。
如果循环链表中的节点数大于1,需要从头遍历循环链表,寻找插入新节点的位置,使得新插入节点后链表仍有序。
可以使用cur和next指针分别表示当前节点和下一个节点,初始时,cur指向头节点,next指向头节点的下一个节点,因为链表长度大于1,所以cur≠next。遍历过程中,判断insertVal能否在cur和next之间插入,如果符合条件就插入,否则就将cur和next都向后移动,直到找到插入新节点的位置或遍历完所有节点。

Python实现
"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    def insert(self, head: 'Node', insertVal: int) -> 'Node':
        node = Node(insertVal)
        if head is None:
            node.next = node
            return node
        if head.next == head:
            head.next = node
            node.next = head
            return head
        cur = head
        next = head.next
        while next != head:
            if cur.val <= insertVal <= next.val:
                break
            if cur.val > next.val:
                if insertVal > cur.val or insertVal < next.val:
                    break
            cur = cur.next
            next = next.next
        cur.next = node
        node.next = next
        return head
Java实现
/*
// 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) {
            node.next = node;
            return node;
        }
        if (head.next == head) {
            head.next = node;
            node.next = head;
            return head;
        }
        Node curr = head, next = head.next;
        while (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;
    }
}
C++实现
/*
// 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;
    }
};
*/

class Solution {
public:
    Node* insert(Node* head, int insertVal) {
        Node *node = new Node(insertVal);
        if (head == nullptr) {
            node->next = node;
            return node;
        }
        if (head->next == head) {
            head->next = node;
            node->next = head;
            return head;
        }
        Node *curr = head, *next = head->next;
        while (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;
    }
};

你可能感兴趣的:(leetcode每日一题,python,java,c++,leetcode)