有头链表
有头链表:用一个固定的假节点来充当头节点(此节点不算作链表长度,仅用于简化算法)
public class Operate { public static void main(String[] args){ Node n1=new Node(101, "熊大"); Node n2=new Node(102, "熊二"); Node n3=new Node(103, "熊三"); Node n4=new Node(104, "熊四"); //加一个假节点 Node head=new Node(0,"假节点"); head.next=n1; n1.next=n2; n2.next=n3; n3.next=n4; n4.next=null; //新建节点插入到假节点之后 Node n5=new Node(105,"熊五"); n5.next=head.next; head.next=n5; //遍历链表内容 Node node=head.next; while(node!=null){ System.out.println(node.id); node=node.next; } } }