楼主,谢谢你的面试经验分享,那道相交链表的,我写了一个做法,你帮我看看是不是这个意思? public class EqualsNode {     private static Node root1 = null;     private static Node root2 = null;     private static Node point1 = new Node(50);     private static Node point2 = new Node(500);     public static void main(String[] args) {         initRoot1();         initRoot2();         printLink(root1);         printLink(root2);         System.out.println(getFirstEqualNode(root1, root2));     }     public static int getFirstEqualNode(Node node1, Node node2) {         if (node1 == null || node2 == null)             return -1;         int node1Index = 0;         int node2Index = 0;         while (node1 != null) {             Node node2Temp = node2;             while (node2 != null) {                 if (node1 == node2) {                     System.out.println("node1Index = " + node1Index + " | node2Index = " + node2Index);                     return node1.value;                 }                 node2 = node2.next;                 node2Index++;             }             node2 = node2Temp;             node1 = node1.next;             node1Index++;             node2Index = 0 ;         }         return -1;     }     public static void printLink(Node root) {         Node tempNode = root;         while (tempNode != null) {             System.out.print(tempNode.value + "\t");             tempNode = tempNode.next;         }         System.out.println();     }     public static void initRoot1() {         root1 = new Node(5);         root1.next = new Node(4);         root1.next.next = point1;         root1.next.next.next = new Node(8);         root1.next.next.next.next = point2;     }     public static void initRoot2() {         root2 = new Node(8);         root2.next = point1;         root2.next.next = new Node(12);         root2.next.next.next = new Node(4);         root2.next.next.next.next = point2;     } } class Node {     int value;     Node next;     public Node(int value) {         this.value = value;     } }
点赞 1

相关推荐

昨天 20:44
武汉大学 Java
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务