【剑指offer-Java版】37两个链表的第一个公共结点
两个链表中的第一个公共顶点:
解法一:两次遍历即可
第一次遍历找到两个链表的长度,求出差值k,然后较长的;链表先走k步,之后两个链表同时走,直到遇到第一个相同的结点为止
解法二:辅助栈,先顺序遍历并将链表内容存储到栈中,然后依次弹栈,直到遇到最后一个不同结点
public class _Q37<T> {public ListNode<T> FindFirstCommonNode(ListNode<T> list1, ListNode<T> list2){if(list1 == null || list2 == null) return null;ListNode<T> node1 = list1;ListNode<T> node2 = list2;int len1 = 0;int len2 = 0;while(node1 != null) {node1 = node1.next; len1++;}while(node2 != null) {node2 = node2.next; len2++;}node1 = list1;node2 = list2;int k = 0;if(len1 >= len2){k = len1 - len2;while(k > 0){ node1 = node1.next; --k;}}else{k = len2 - len1;while(k > 0){ node2 = node2.next; --k;}}while (node1 != null && node2 != null && node1.value != node2.value) {node1 = node1.next;node2 = node2.next;}return node1; // node可能为空,因为可能输入链表中并没有相同结点}}
测试代码:
public class _Q37Test extends TestCase {_Q37<Integer> firstCommon = new _Q37<Integer>();public void test(){// 一旦第一个相同,后面都必须相同int array1[] = {1, 2, 3, 4, 5, 6};int array2[] = {6};int array3[] = {7, 8, 9, 10};ListNode<Integer> list1 = new ListNode<Integer>();ListNode<Integer> list2 = new ListNode<Integer>();ListNode<Integer> list3 = new ListNode<Integer>();ListNode<Integer> p = list1;for(int i=0; i<array1.length; i++){ListNode<Integer> node = new ListNode<>();node.value = array1[i];node.next = p.next;p.next = node;p = p.next;}p = list2;for(int i=0; i<array2.length; i++){ListNode<Integer> node = new ListNode<>();node.value = array2[i];node.next = p.next;p.next = node;p = p.next;}p = list3;for(int i=0; i<array2.length; i++){ListNode<Integer> node = new ListNode<>();node.value = array3[i];node.next = p.next;p.next = node;p = p.next;}System.out.println(firstCommon.FindFirstCommonNode(list1.next, list2.next).value);if(firstCommon.FindFirstCommonNode(list1.next, list3.next) != null){System.out.println(firstCommon.FindFirstCommonNode(list1.next, list3.next).value);}else{System.out.println("null");}}}
【剑指offer-Java版】37两个链表的第一个公共结点相关推荐
- 剑指offer面试题[37]-两个链表的第一个公共结点
题目描述 输入两个链表,找出它们的第一个公共结点. /* struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), ...
- 剑指offer(C++)-JZ52:两个链表的第一个公共结点(数据结构-链表)
作者:翟天保Steven 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处 题目描述: 输入两个无环的单向链表,找出它们的第一个公共结点,如果没有公共节点则返回空.(注意因 ...
- 《剑指Offer》52:两个链表的第一个公共节点
题目 输入两个链表,找出它们的第一个公共节点. public static class ListNode{public int val;public ListNode next;public List ...
- 剑指offer面试题52. 两个链表的第一个公共节点(双指针法)
题目描述 输入两个链表,找出它们的第一个公共节点. 思路 详见链接 代码 class Solution:def getIntersectionNode(self, headA:ListNode, he ...
- 37. 两个链表的第一个公共结点
为什么80%的码农都做不了架构师?>>> 题目:输入两个链表,找出它们的第一个公共结点. 思路:先遍历两个链表得出它们各自的长度,然后让长链表先走,直到长度和短的一致,然后两个 ...
- 剑指offer java版 test3—从尾到头打印链表
标题:剑指offer java版 test3-从尾到头打印链表 题目:输入一个链表,按链表从尾到头的顺序返回一个ArrayList. 解答:知识不够全面,用ArrayList做的 但是看到大佬们还可以 ...
- 剑指offer——面试题37:两个链表的第一个公共结点
剑指offer--面试题37:两个链表的第一个公共结点 20180906整理 Solution1: 时间复杂度为O(n2)O(n2)O(n^2)的垃圾算法 /* struct ListNode {in ...
- 《剑指Offer》Java刷题 NO.36 两个链表的第一个公共结点(链表,等长拼接法,长者先行法,辅助栈)
<剑指Offer>Java刷题 NO.36 两个链表的第一个公共结点(链表,等长拼接法,长者先行法,辅助栈) 传送门:<剑指Offer刷题总目录> 时间:2020-06-19 ...
- [剑指offer]面试题37:两个链表的第一个公共结点
面试题37:两个链表的第一个公共结点 题目:输入两个链表,找出它们的第一个公共结点.链表结点定义如下: struct ListNode {int val;ListNode *next;ListNode ...
最新文章
- 【NIO】缓存区buffer
- MFC程序的框架理解
- Spring Autowire自动装配
- 重拾C,一天一点点_3
- kong 使用jwt RSA256证书
- 测量人看过来:多种语言编写的测量坐标反算神器附源码(C#/VB)
- Oracle中SQL语句学习五(统计分组语句group by和having)
- CGLib动态代理原理及实现
- spring 标注 详解
- 含有空格或者逗号的字符串反转最有效的办法——栈
- iPhone企业应用实例分析之二:程序处理流程
- linux 编写 声卡驱动程序,在Linux中注册声卡驱动程序
- 抖音小程序二维码生成
- org.apache.flink.util.FlinkRuntimeException: Exceeded checkpoint tolerable failure threshold
- python调用shell命令
- 微信公众号订阅号申请注册教程
- 【开发技巧】-- 一篇女朋友也能看懂的Spring整合第三方支付(微信支付-扫码支付实现篇)
- (踩坑向+靠谱安装贴记录)ubuntu20.04安装freesurfer、FSL、ANTs、MRtrix,虚拟机
- 大学教授课堂上吸烟,不是什么不得了的事!
- [转载]十六进制数的两种不同表示:0x和H