题目:输入两个链表,找出它们的第一个公共结点。

方法一:

       如果两个链表有公共结点,那么公共结点出现在两个链表的尾部。如果从两个链表的尾部开始往前比较,最后一个相同的结点就是要找的结点。

public class ListNode{
     int value;
     ListNode next;
    
     public ListNode(int data,ListNode node){
          this.value=data;
          this.next=node;
     }
}
import java.util.Stack;

public class Solution{
     public static ListNode findFirstCommonNode(ListNode node1,ListNode node2){
           if(node1==null||node2==null){
                return null;
           }
           Stack<ListNode> stack1=new Stack<>();
           Stack<ListNode> stack2=new Stack<>();
           while(node1!=null){
                stack1.push(node1);
                node1=node1.next;
           }
           while(node2!=null){
                stack2.push(node2);
                node2=node2.next;
           }
           ListNode tempNode=null;
           while((!stack1.empty())&&(!stack2.empty())){
                 ListNode n1=stack1.pop();
                 ListNode n2=stack2.pop();
                 if(n1!=n2){
                      break;
                 }
                 tempNode=n1;
           }
           return tempNode;
     }

     public static void main(String[] args){
          ListNode node7=new ListNode(7,null);
          ListNode node6=new ListNode(6,node7);
          ListNode node5=new ListNode(5,node6);
          ListNode node4=new ListNode(4,node5);
          ListNode node3=new ListNode(3,node6);
          ListNode node2=new ListNode(2,node3);
          ListNode node1=new ListNode(1,node2);
          ListNode node=findFirstCommonNode(node1,node4);
          if(node!=null){
              System.out.println(node.value);
          }
    }      
}

方法二(效率高):

        之所以需要用到栈,是因为同时遍历到两个栈的尾结点。当两个链表的长度不相同时,如果从开头遍历到达尾结点的时间就不一致。其实有更好的方法:首先遍历两个链表得到它们的长度。在第二次遍历的时候,在较长的链表上先走若干步,接着同时在两个链表上遍历,找到的第一个相同的结点就是它们的第一个公共结点。

public class Solution{
     public static ListNode findFirstCommonNode(ListNode node1,ListNode node2){
          if(node1==null||node2==null){
               return null;
          }
          int len1=getListLength(node1);
          int len2=getListLength(node2);
          while(len1>len2){
               node1=node1.next;
               len1--;
          }
          while(len1<len2){
               node2=node2.next;
               len2--;
          }
          ListNode tempNode=null;
          while(node1!=null&&node2!=null){
               if(node1==node2){
                     tempNode=node1;
                     break;
               }     
               node1=node1.next;
               node2=node2.next;
          }
          return tempNode;
     }

     public static int getListLength(ListNode node){
           int len=0;
           while(node!=null){
                len++;
                node=node.next;
           }
           return len;
     }

     public static void main(String[] args){
           ListNode node7=new ListNode(7,null);
           ListNode node6=new ListNode(6,node7);
           ListNode node5=new ListNode(5,node6);
           ListNode node4=new ListNode(4,node5);
           ListNode node3=new ListNode(3,node6);
           ListNode node2=new ListNode(2,node3);
           ListNode node1=new ListNode(1,node2);
           ListNode node=findFirstCommonNode1(node1,node4);
           if(node!=null){
               System.out.println(node.value);
           }
     }
}

 

 posted on 2018-11-22 21:49  会飞的金鱼  阅读(101)  评论(0)    收藏  举报