初学Java--关于链表(ps:待整理)
class Node{
public int value;
public Node next;
public Node(int value){
this.value=value;
this.next=null;
}
}
/*
public class Linklist{
// travel
public static Node traVel(Node head){
Node cur=head;
while(cur!=null){
cur=cur.next;
}
}
public static Node pushFront(Node head,int value){
Node newNode=new Node(value);
newNode.next=head;
return newNode;
}
public static Node pushBack(Node head,int value){
Node newNode=new Node(value);
if(head==null){
newNode.next=head;
return newNode;
}else{
Node last=getLast(head);
last.next=newNode;
return head;
}
}
public static Node getLast(Node head){
Node cur=head;
while(cur.next!=null){
return cur.next;
}
}
public static Node popBack(Node head){
if(head==null){
System.out.println("参数不合法");
return null;
}
if(head.next==null){
return null;
}
}
public static void display(Node head){
for(Node cur=head;cur!=null;cur=cur.next){
System.out.printf("%d-->",cur.value);
}
System.out.pritln("null");
}
//please learn how to write the code not to copy code,try to
public static Node pushFront(Node head,int value){
//1、创建结点
//2、让新结点的next指向head(头结点)
//3、更新头结点的引用 不能用head=newNode ,head是形参
Node newNode=new Node(value);
newNode.next=head;
return newNode;
}
public static Node getLast(Node head){
}
public static Node popBack(Node head){
//找到倒数第二个结点 next.next ==null
//倒数第二个的节点指向null
//把最后一个节点内存释放 GC会负责
//更新头结点
// 1.none
// 2.one
if(head==null){
System.out.println("不合法");
return null;
}
// 头删
if(head.next==null){
return null;//or return head.next
}
Node c=
}
public static void main(String[] args){
head=pushFront(1);
head=pushFront(2);
head=pushBack(3);
display(head);
}
}
*/
关于链表我有很多代码都还及时没有练习。。。