写点什么

第八周

用户头像
等燕归
关注
发布于: 2020 年 11 月 15 日
public class Test1 {    public class Node{        //要包装的数据        private String data;        //下一个节点        private Node next;
public Node(String data) { this.data = data; }
public String getData() { return data; }
public void setData(String data) { this.data = data; }
public Node getNext() { return next; }
public void setNext(Node next) { this.next = next; } }
public static void main(String[] args) { //链表初始化 Test1 test1 = new Test1(); Test1.Node nodeA = test1.new Node("a"); Test1.Node nodeB = test1.new Node("b"); nodeA.setNext(nodeB);
Test1.Node nodeD = test1.new Node("d"); Test1.Node nodeE = test1.new Node("e"); Test1.Node nodeF = test1.new Node("f"); nodeD.setNext(nodeE); nodeE.setNext(nodeF);
Test1.Node nodeX = test1.new Node("x"); Test1.Node nodeY = test1.new Node("y"); Test1.Node nodeZ = test1.new Node("z"); nodeX.setNext(nodeY); nodeY.setNext(nodeZ);
nodeB.setNext(nodeX); nodeF.setNext(nodeX);
Test1.Node node1 = nodeA;
while (node1 != null){ Test1.Node node2 = nodeD; while (node2 != null){ if (node1.getData().equals(node2.getData())){ System.out.println("节点合并,合并元素为 -> " + node1.getData()); } node2 = node2.getNext(); } node1 = node1.getNext(); }
}
}
复制代码

时间复杂度为 T(n) = O(n^2)

************************************************************************************************


用户头像

等燕归

关注

还未添加个人签名 2019.03.29 加入

还未添加个人简介

评论

发布
暂无评论
第八周