leetcode之Intersection of Two Linked Lists
来源:互联网 发布:淘宝商城女装晚礼服 编辑:程序博客网 时间:2024/05/17 02:45
(1)C语言实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
struct ListNode* p1 = headA;
struct ListNode* p2 = headB;
if(p1==NULL || p2 == NULL)
return NULL;
while(p1!=p2){
p1 = p1->next;
p2 = p2->next;
if(p1==p2)
return p1;
if(p1==NULL)
p1=headB;
if(p2==NULL)
p2=headA;
}
return p1;
}
(2)C++实习
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* p1 = headA;
ListNode* p2 = headB;
if(headA == NULL || headB == NULL){
return NULL;
}
while(p1!=p2){
p1 = p1->next;
p2 = p2->next;
if(p1 == p2){
return p1;
}
if(p1 == NULL){
p1 = headB;
}
if(p2 == NULL){
p2 = headA;
}
}
return p1;
}
};
(3)java实现
/**
* 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) {
ListNode p1 = headA;
ListNode p2 = headB;
if(p1==null || p2==null)
return null;
while(p1!=p2){
p1 = p1.next;
p2 = p2.next;
if(p1==p2)
return p1;
if(p1==null)
p1 = headB;
if(p2 == null)
p2 = headA;
}
return p1;
}
}
- leetcode之Intersection of Two Linked Lists
- LeetCode之Intersection of Two Linked Lists
- LeetCode 之 Intersection of Two Linked Lists
- leetcode之Intersection of Two Linked Lists
- leetcode之Intersection of Two Linked Lists
- Leetcode之Intersection of Two Linked Lists
- leetcode之Intersection of Two Linked Lists
- Leetcode 之 Intersection of Two Linked Lists
- [leetcode] [leetcode]Intersection of Two Linked Lists
- 【LeetCode】Intersection of Two Linked Lists
- LeetCode: Intersection of Two Linked Lists
- [Leetcode] Intersection of Two Linked Lists
- [Leetcode]Intersection of Two Linked Lists
- LeetCode:Intersection of Two Linked Lists
- leetcode: Intersection of Two Linked Lists
- Leetcode: Intersection of Two Linked Lists
- Intersection of Two Linked Lists--leetcode
- [leetcode]Intersection of Two Linked Lists
- rope大法好
- 如何取出一个整数的每位数字
- storm的并发
- Web开发过程中乱码问题
- Redis+linux入门(二)
- leetcode之Intersection of Two Linked Lists
- Github+Jekyll —— 创建个人免费博客(三)Git学习
- 最长滑雪路径
- 自定义控件---OnMeasure()方法分析
- 【GDOI2016模拟3.9】暴走的图灵机
- 二叉树是否相同
- UIWebView简单使用
- nodeJs grunt中Gruntfile.js模版
- 表单提交方式总结