【資料結構】28、判斷連結串列是否有環
阿新 • • 發佈:2019-06-24
因為最近小米電話面試被問到如何判斷一個連結串列是否有環,那今天正好實現以下這個演算法
1.連結串列
package y2019.Algorithm.LinkedList; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.LinkedList * @ClassName: FindRing * @Author: xiaof * @Description: 現在存在一條連結串列,尋找這個連結串列是否存在環 * @Date: 2019/6/24 9:12 * @Version: 1.0 */ public class FindRing { public class Node { private String key; private String value; private Node next; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } } //連結串列頭結點 private Node ringHead; private Node ringTail; private int length; public FindRing() { ringHead = ringTail = null; } //建立一個長度為n的連結串列 public FindRing(int length) { ringHead = ringTail = null; Node curNode = ringHead; for(int i = 0; i < length; ++i) { if(ringHead == null) { ringHead = new Node(); ringHead.key = i + " " + System.currentTimeMillis(); curNode = ringHead; ringTail = curNode; } else if (curNode.next == null) { curNode.next = new Node(); curNode = curNode.next; curNode.key = i + " " + System.currentTimeMillis(); ringTail = curNode; } } this.length = length; } public int size() { return length; } public void add(Node newNode) { //新增節點進入連結串列 //尾部插入 if(ringTail == null) { ringTail = newNode; ringTail = ringTail.next; } else { Node temp = ringTail.next; temp = newNode; ringTail = temp.next; } ++length; } public Node getByIndex(int index) { Node resultNode = ringHead; for(int i = 1; i < index; ++i) { resultNode = resultNode.next; } return resultNode; } public Node getRingHead() { return ringHead; } public void setRingHead(Node ringHead) { this.ringHead = ringHead; } public Node getRingTail() { return ringTail; } public void setRingTail(Node ringTail) { this.ringTail = ringTail; } public int getLength() { return length; } public void setLength(int length) { this.length = length; } }
內部類的使用,是為了方便除錯
測試類:
package LinkedList; import org.junit.jupiter.api.Test; import y2019.Algorithm.LinkedList.FindRing; import java.util.HashSet; import java.util.Set; /** * @ProjectName: cutter-point * @Package: LinkedList * @ClassName: Test1 * @Author: xiaof * @Description: ${description} * @Date: 2019/6/24 9:31 * @Version: 1.0 */ public class Test1 { @Test public void test1 () { //判斷一個連結串列是否有環 //1.實現連結串列set方式,也就是通過hashcode進行定位 FindRing findRing = new FindRing(15); //建立環節點 int ringIndex = (int) (Math.random() * 10); ringIndex = 5; FindRing.Node ringNode = findRing.getByIndex(ringIndex); FindRing.Node tailNode = findRing.getRingTail(); tailNode.setNext(ringNode); //開始判斷是否有環 Set set = new HashSet(); //迴圈遍歷連結串列,直到得到重複的 FindRing.Node indexNode = findRing.getRingHead(); int indexCount = 0; while(true) { if(indexNode == null) { System.out.println("無環"); break; } if(set.contains(indexNode)) { System.out.println("有環,key:" + indexNode.getKey() + " 遍歷次數:" + indexCount); break; } set.add(indexNode); indexNode = indexNode.getNext(); indexCount += 1; } System.out.println("結束遍歷"); } @Test public void test2 () { //判斷一個連結串列是否有環 //1.通過2個指標同時查詢,直到兩個指標指向同一個節點作為有環,如果結束,那麼無換 FindRing findRing = new FindRing(15); //建立環節點 int ringIndex = (int) (Math.random() * 10); ringIndex = 5; FindRing.Node ringNode = findRing.getByIndex(ringIndex); FindRing.Node tailNode = findRing.getRingTail(); tailNode.setNext(ringNode); //開始判斷是否有環 FindRing.Node index1 = findRing.getRingHead(); FindRing.Node index2 = findRing.getRingHead().getNext(); int indexCount = 0; int indexCount2 = 0; //第一個每次遍歷一個,第二個每次遍歷2個 while (true) { if(index1 == null || index2 == null || index2.getNext() == null) { System.out.println("無環"); break; } if(index1 == index2) { System.out.println("有環,key:" + index1.getKey() + " 遍歷次數1:" + indexCount + " 遍歷次數2:" + indexCount2); break; } indexCount += 1; indexCount2 += 2; index1 = index1.getNext(); index2 = index2.getNext().getNext(); } System.out.println("結束遍歷"); } }
結果:
測試一:
測試二:
從這裡我們可以判斷到第二個方法並不能定位到產生環的節點是哪個節點,並且迴圈次數比第一個多
總結:
方式一:對空間佔用比較大,但是時間複雜度底,並且可以定位到產生環節點的位置
方式二:對空間佔用比較小,但是時間複雜度高,並且無法定位到具體產生環的節點
&n