zoukankan      html  css  js  c++  java
  • 两个链表的第一个公共结点

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

    实现语言:Java

    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    import java.util.Stack;
    public class Solution {
        public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
            if(pHead1==null||pHead2==null){
                return null;
            }
            Stack<ListNode> stk1=new Stack<ListNode>();
            Stack<ListNode> stk2=new Stack<ListNode>();
            while(pHead1!=null){
                stk1.push(pHead1);
                pHead1=pHead1.next;
            }
            while(pHead2!=null){
                stk2.push(pHead2);
                pHead2=pHead2.next;
            }
            ListNode commonNode=null;
            while(!stk1.isEmpty()&&!stk2.isEmpty()&&stk1.peek()==stk2.peek()){
                stk1.pop();
                commonNode=stk2.pop();
            }
            return commonNode;
        }
    }
    

     实现语言:Java

    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    import java.util.HashMap;
    public class Solution {
        public ListNode FindFirstCommonNode(ListNode head1, ListNode head2) {
            if(head1==null||head2==null){
                return null;
            }
            HashMap<ListNode,ListNode> map=new HashMap<ListNode,ListNode>();
            while(head1!=null){
                map.put(head1,head1);
                head1=head1.next;
            }
            while(head2!=null){
                if(map.containsKey(head2)){
                    return head2;
                }
                head2=head2.next;
            }
            return null;
        }
    }

    实现语言:Java

    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    
    public class Solution {
        public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
            if(pHead1==null||pHead2==null){
                return null;
            }
            ListNode cur=pHead1;
            int n=0;
            while(cur!=null){
                ++n;
                cur=cur.next;
            }
            cur=pHead2;
            while(cur!=null){
                --n;
                cur=cur.next;
            }
            ListNode shortList=null;
            ListNode longList=null;
            if(n<0){
                shortList=pHead1;
                longList=pHead2;
            }else{
                shortList=pHead2;
                longList=pHead1;
            }
            n=n<0?-n:n;
            for(int i=0;i<n;++i){
                longList=longList.next;
            }
            while(shortList!=null&&longList!=null&&shortList.val!=longList.val){
                shortList=shortList.next;
                longList=longList.next;
            }
            return longList;
        }
    }
    
  • 相关阅读:
    漫谈C语言结构体
    如何理解c和c++的复杂类型声明
    STM32 时钟系统
    STM32 系统架构
    运放参数的详细解释和分析-part1,输入偏置电流和输入失调电流【转】
    ROM、RAM、DRAM、SRAM、FLASH的区别?
    FATFS模块应用笔记
    关于I2C和SPI总线协议【转】
    USB编程概念
    Ubuntu手机识别
  • 原文地址:https://www.cnblogs.com/xidian2014/p/10198734.html
Copyright © 2011-2022 走看看