0
点赞
收藏
分享

微信扫一扫

单链表的while循环创建

草原小黄河 2022-04-30 阅读 46
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 条评论