单链表的while循环创建

阅读 48

2022-04-30

public static ListNode createLink(){
        ListNode root=new ListNode();
        ListNode cursor=root;
        int i=0;
        while( i<3){
            ListNode node = new ListNode(i);
            cursor.next=node;
            cursor=node;
            i++;
        }
        return  root.next;
    }

root作为头结点,此节点作为创建逻辑的补充,没具体含义.

cursor理解为末端节点指针,当有新的节点被插入,通过该指针找到插入位置,并后移一位

精彩评论(0)

0 0 举报