//向有序的环形单链表中插入新节点
public class InsertNode{
//单链表节点的定义
public static class Node{
public int value;
public Node next;
public Node(int data)
{
this.value=data;
}
}
//向有序的环形单链表中插入新节点
public static Node insertNode(Node head,int num)
{
if(head==null)
{
return new Node(num);
}
//(1)开头节点插入新节点,并且构成环形链表
if(head.value>num)
{
Node node=new Node(num);
node.next=head;
Node p=head; //记录先前的头结点
while(head.next!=p)
{
head=head.next;
}
head.next=node; //最后一个节点指向新插入的头节点
return node;
}
//(2)中间插入
Node p=head.next;
向有序的环形单链表中插入新节点
最新推荐文章于 2022-01-15 14:28:28 发布