160. Intersection of Two Linked Lists
2016-06-10 21:29
246 查看
题目:https://leetcode.com/problems/intersection-of-two-linked-lists/
代码:
代码:
/** * 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; if(headA==headB) return headA; ListNode ta,tb; ta = headA; int lengtha=1,lengthb=1; while(ta.next!=null) { lengtha++; ta = ta.next; } tb = headB; while(tb.next!=null) { lengthb++; tb = tb.next; } if(ta!=tb) return null; ta = headA; tb = headB; while(lengtha>lengthb) { ta = ta.next; lengtha--; } while(lengtha<lengthb) { tb = tb.next; lengthb--; } while(ta!=null) { if(ta==tb) return ta; ta = ta.next; tb = tb.next; } return null; } } 2ms
相关文章推荐
- Qt 技巧: 解决未解析的SSL问题
- android activity知识总结
- nginx 配置优化的几个参数
- 219. Contains Duplicate II
- 逃不过的fork()
- JSP和Servlet的中文乱码处理
- Qt 代码: 子窗口调用父窗口(其实就是用指针直接访问)
- 从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别
- Android Studio教程(六)之AS插件
- SQLite帮助类SQlitehelper 实现对SQLite数据的增删改查
- 一些计算机网络协议
- nginx负载均衡简单配置
- HDU2019
- 在MFC状态栏显示时间
- 互联网中的视频/音频服务
- poj3678 Katu Puzzle
- Qt浅译:JSON Support in Qt(JSON只有六种数据类型)
- 2016年第8本:不可思议的心理控制实验
- 【CSS笔记四】CSS的继承、层叠和特殊性
- JavaScript如何处理错误