首先定義自定義結點類,存儲節點信息:
public class Node { Node next=null; int data; public Node(int data){ this.data=data; } }
獲取鏈表長度:
private int length() { int length=0; Node temp=head; while(temp!=null){ length++; temp=temp.next; } return length; }
打印鏈表:
public void printMyList(){ Node temp=this.head; while(temp!=null){ System.out.print(temp.data); temp=temp.next; if(temp!=null){ System.out.print("-->"); }else{ System.out.println(); } } }
向鏈表中插入數據:
public void addNode(int d){ Node node=new Node(d); if(head==null){ head=node; return; } Node temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=node; }
向鏈表中插入結點:
public void addNode(Node node){ if(head==null){ head=node; return; } Node temp=head; while(temp.next!=null){ temp=temp.next; } temp.next=node; }
在鏈表尾部添加另一個鏈表:
public static void addList(MyLinkedList list,MyLinkedList afterlist) { Node thishead = list.head; Node addhead = afterlist.head; if(thishead==null){ thishead=addhead; return; } Node temp=thishead; while(temp.next!=null){ temp=temp.next; } temp.next=addhead; }
從鏈表中刪除指定位置的數據:
public boolean deleteNode(int index){//index:刪除的元素的位置 if(index<1||index>length()){ return false; } if(index==1){ head=head.next; return true; } int i=2; Node preNode=head; Node curNode=preNode.next; while(curNode!=null){ if(i==index){ preNode.next=curNode.next; return true; } preNode=curNode; curNode=curNode.next; i++; } return true; }
對鏈表進行排序,返回排序后的頭結點:
public Node orderList(){ Node nextNode=null; int temp=0; Node headNode=head; while(headNode.next!=null){ nextNode=headNode.next; while(nextNode!=null){ if(headNode.data>nextNode.data){ temp=nextNode.data; nextNode.data=headNode.data; headNode.data=temp; } nextNode=nextNode.next; } headNode=headNode.next; } return head; }
從鏈表中刪除重復數據 第一種方法
public void deleteRepetition1(){ Hashtable<Integer, Integer>hashtable=new Hashtable<>(); Node temp=this.head; Node pre=null; while(temp!=null){ if(hashtable.containsKey(temp.data)) { pre.next=temp.next; } else { hashtable.put(temp.data, 1); pre=temp; } temp=temp.next; } }
從鏈表中刪除重復數據 第二種方法:
public void deleteRepetition2(){ Node temp=head; while(temp!=null){ Node i=temp; while(i.next!=null){ if(temp.data==i.next.data) { i.next=i.next.next; } else { i=i.next; } } temp=temp.next; } }
找出單鏈表中的倒數第k個元素:
public Node findLastElem(int k){ if(k<1){ System.out.println("k不合法"); return null; } if(head==null){ System.out.println("鏈表不包含元素"); return null; } Node p1=head; Node p2=head; for(int i=0;i<k-1;i++){ p2=p2.next; } while(p2.next!=null){ p1=p1.next; p2=p2.next; } return p1; }
鏈表反轉:
public void reversal(){ Node pReversalHead=this.head; Node pNode=this.head; Node pPrev=null; while(pNode!=null){ Node pNext=pNode.next; if(pNext==null) { pReversalHead=pNode; } pNode.next=pPrev; pPrev=pNode; pNode=pNext; } this.head=pReversalHead; }
不反轉鏈表,倒序輸出鏈表元素:
public void printReversalList(Node pHead){ if(pHead.next==null){ System.out.print(pHead.data); } if(pHead.next!=null){ printReversalList(pHead.next); System.out.print("-->"+pHead.data); } }
尋找單鏈表中間節點:
public Node searchMidNode(){ Node pNode=this.head; Node qNode=this.head; while(pNode!=null&&pNode.next!=null&&pNode.next.next!=null){ pNode=pNode.next.next; qNode=qNode.next; } return qNode; }
判斷一個鏈表是否有環:
public boolean isHaveLoop(){ Node fast=head; Node slow=head; if(fast==null){ return false; } while(fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next; if(fast==slow){ return true; } } return false; }
尋找環入口:
public Node getLoopStart(MyLinkedList list){ //在鏈表頭和相遇點分別設置一個指針,每次各走一步,兩個指針必定相遇,且第一個相遇點為環入口 Node slow=list.head; Node fast=list.head; while(slow.next!=null&&fast.next!=null){ slow=slow.next; fast=fast.next.next; if(slow==fast){ break; } } while(slow!=fast){ slow=slow.next; fast=fast.next; } return slow; }
在不知道頭指針的情況下刪除指定節點:
public boolean deleteNode(Node node){ //此方法不能刪除鏈表最后一個節點,因為無法使其前驅結點的next指向null; if(node==null||node.next==null){ return false; } node.data=node.next.data; node.next=node.next.next; return true; }
判斷兩個鏈表是否相交:
public static boolean isIntersect(MyLinkedList list,MyLinkedList list2){ Node head1=list.head; Node head2=list2.head; if(head1==null||head2==null){ return false; } while(head1.next!=null){ head1=head1.next; } while(head2.next!=null){ head2=head2.next; } return head1==head2; }
尋找兩個鏈表相交的第一個節點:
public static Node getFirstMeetNode(MyLinkedList list,MyLinkedList list2){ Node head1=list.head; Node head2=list2.head; if (isIntersect(list, list2)==false){ return null; } Node t1=head1; Node t2=head2; if(list.length()>list2.length()){ int d=list.length()-list2.length(); while(d!=0){ t1=t1.next; d--; } } else { int d=list2.length()-list.length(); while(d!=0){ t2=t2.next; d--; } } while(t1!=t2){ t1=t1.next; t2=t2.next; } return t1; }