Java 鏈表常見考題總結,java考題總結

來源:互聯網
上載者:User

Java 鏈表常見考題總結,java考題總結

首先定義自訂結點類,儲存節點資訊:

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;    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.