LeetCode——Intersection of Two Linked Lists
2015-06-19 23:11
387 查看
Description:
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
begin to intersect at node c1.
求两个链表的相交的部分
Write a program to find the node at which the intersection of two singly linked lists begins.
For example, the following two linked lists:
A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3
begin to intersect at node c1.
求两个链表的相交的部分
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA==null || headB==null) return null; ListNode aNode = headA; ListNode bNode = headB; int aLen = 0, bLen = 0; while(aNode != null) { aLen ++; aNode = aNode.next; } while(bNode != null) { bLen ++; bNode = bNode.next; } if(aNode != bNode) return null; if(aLen > bLen) { for(int i = 0; i < aLen-bLen; i++) headA = headA.next; } else if(aLen < bLen) { for(int i=0; i<bLen-aLen; i++) headB = headB.next; } while(headA != headB) { headA = headA.next; headB = headB.next; } return headA; } }
相关文章推荐
- Python学习笔记(5)---ESETful
- cdn及域名解析原理
- 我对软件测试的理解以及我的职业规划
- “<meta http-equiv="X-UA-Compatible" content="IE=7, edge"/>” 的意义
- C#数据绑定(续)
- <meta>标签总结
- 补第一阶段冲刺站立会议1(应发表日期5月13日)
- 14Spring通过注解配置Bean(2)
- React.js终探(六)
- 记录一个c#计算器程序
- leetcode--Evaluate Reverse Polish Notation
- Java7 ForkJoin 框架
- 线性回归
- Ubuntu下安装最新版本的CMake
- 人,绩效和道德读后感
- spring MVC 管理HttpClient---实现在java中直接向Controller发送请求
- spring MVC 管理HttpClient---实现在java中直接向Controller发送请求
- ArcMap2Sld:一个将MXD中图层配图样式转换为OGC的SLD文件的开源工具
- 摄影插画精选
- dom4j解析soap型消息xml