package Test2;
import Test.Node;
/**
-
Created by Administrator on 2019/4/28.
*/
public class LinkList {
class Node{
private int data;
private Node next;
public Node(int data){
this.data=data;
}
}
private Node head;
public void add(int data){
Node newNode=new Node(data);
if(headnull){
head=newNode;
return ;
}
Node temp = head;
while (temp.next!=null){
temp=temp.next;
}
temp.next=new Node(data);
}
//遍历链表
public void print(Node node){
if(nodenull){
return;
}
Node temp = node;
while (temp!=null){
System.out.print(temp.data+",");
temp=temp.next;
}
}public static void main(String[] args){ LinkList list=new LinkList(); for(int i=0;i<=5;i++){ list.add(i); } list.print(list.head); System.out.println("head.data:" + list.head.data); int length=list.getLength(); System.out.println("length:"+length); Node newHead= list.reverseList(list.head); list.print(newHead); } //获取链表长度
public int getLength(){
int length=0;
Node temp =head;
while (temp!=null){
length++;
temp=temp.next;
}
return length;
}
//删除指定节点
public boolean deleteNode(Node node){
Node cur=head;
Node pre=head;
while (cur.data!=node.data){
if(cur.next==null){
return false;
}else {
pre=cur;
cur=cur.next;
}
}
//如果是头节点
if (cur.equals(head)){
head=cur.next;
}else {
pre.next=cur.next;
}
return true;
}
//链表反转
public Node reverseList(Node head){
Node pre=null;
Node next=null;
while (head!=null){
next=head.next;
head.next=pre;
pre=head;//指针后移
head=next;
}
return pre;}
//判断链表是否有环环
public boolean hasCycle(Node head){
if(head==null){
return false;
}
Node first=null;
Node second=null;
while (head.next!=null){} return true;
}
}