判斷單鏈表是否有環——LinkedList

很久沒有看數據結構相關內容,突然面試官讓寫判斷鏈表是否有環,居然寫了半天寫不出來,汗~java

知道本身有多渣,就好好學習吧node

接下來開始寫判斷鏈表是否有環的方法吧面試

1.首先定一個結點類Node數據結構

package com.example.demo;

public class Node<T> {
    private T val;
    private Node<T> next;

    public Node(T object) {
        this.val=object;
    }

    public void Node(T val) {
        this.val = val;
    }

    public void setVal(T val){
        this.val=val;
    }

    public T getVal(){
        return this.val;
    }


    public Node<T> getNext(){
        return this.next;
    }

    public void setNext(Node<T> node) {
        this.next = node;
    }
    
}

2.而後鏈表類單元測試

package com.example.demo;


import java.util.List;

public class LinkList {
    private Node root;
    private List<Node> list;


    public LinkList() {

    }


    public Node getRoot(){
        return this.root;
    }

    public List<Node> getList() {
        return list;
    }

    public void setList(List<Node> list) {
        this.list = list;
    }

    public void setRoot(Node node) {
        this.root = node;
    }


    public boolean isExistRound(Node root){
        if(root==null){
            return false;
        }
        if(root.getNext()==null){
            return false;
        }
        Node p=root;
        Node q=root.getNext();

        while(q!=null){
            if(p==q){
                return true;
            }
            q=q.getNext();
        }
        return false;
    }


}

3.而後是單元測試學習

package com.example.demo;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class LinkListTest {


    @Test
    public void should_return_true_when_link_is_round() {
        String str = "1-2-3-4-5-6";
        LinkList roundlinkList = createRoundLinkList(str);
        Assert.assertEquals(true, roundlinkList.isExistRound(roundlinkList.getRoot()));
        LinkList notRoundLinkList=createNotRoundLinkList(str);
        Assert.assertEquals(false, notRoundLinkList.isExistRound(notRoundLinkList.getRoot()));


    }

    @Test
    public void should_return_linked_list_value() {
        String str = "1-2-3-4-5";
        LinkList linkList = createNotRoundLinkList(str);
        Node node = linkList.getRoot();
        while (node != null) {
            System.out.println(node.getVal() + " ");
            node = node.getNext();
        }
    }

    public LinkList createRoundLinkList(String str) {
        LinkList linkList = createNotRoundLinkList(str);
        List<Node> nodeList = linkList.getList();
        nodeList.get(nodeList.size()-1).setNext(nodeList.get(0));
        linkList.setList(nodeList);
        return linkList;
    }


    public LinkList createNotRoundLinkList(String str) {
        LinkList linkList = new LinkList();
        List<Node> nodeList = new ArrayList<>();
        Object[] objects = str.split("-");
        for (int i = 0; i < objects.length; i++) {
            nodeList.add(new Node<>(objects[i]));
        }
        linkList.setRoot(nodeList.get(0));
        for (int j = 0; j < nodeList.size() - 1; j++) {
            nodeList.get(j).setNext(nodeList.get(j + 1));
        }
        linkList.setList(nodeList);
        return linkList;
    }
}
相關文章
相關標籤/搜索