利用两个栈,然后分别存储每一个链表。

继而,相继pop相同的节点。

有些细节需要注意,请看最后的返回值是如何处理的。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#define MAX 100000
typedef struct Stack{
struct ListNode *array[MAX];
int top;
}Stack;
struct ListNode *get_top(Stack s){
return s.array[s.top-1];
}
struct ListNode *pop(Stack *s){
return s->array[--(s->top)];
}
void push(Stack *s,struct ListNode *p){
s->array[s->top++]=p;
}
int empty(Stack s){
return(s.top==0);
}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
Stack s1,s2;
struct ListNode *p; s1.top=0,s2.top=0; p=headA;
while(p!=NULL){
push(&s1,p);
p=p->next;
}
p=headB;
while(p!=NULL){
push(&s2,p);
p=p->next;
}
while(!empty(s1)&&!empty(s2)){
if(get_top(s1)==get_top(s2))
{
pop(&s1);
pop(&s2);
}
else break;
}
if(headA||headB){
if(!empty(s1))return (get_top(s1)->next);
else if(!empty(s2))return get_top(s2)->next;
else return headA;
}
return NULL;//两个链表都为空的话返回NULL
}
自己写的栈结构,所以代码有点长。
Any problems contact me.

  

Intersection of Two Linked Lists | LeetCode的更多相关文章

  1. [LeetCode] 160. Intersection of Two Linked Lists 解题思路

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. LeetCode: Intersection of Two Linked Lists 解题报告

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  3. [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)

    Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...

  4. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  5. 2016.5.24——Intersection of Two Linked Lists

    Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...

  6. LeetCode_160. Intersection of Two Linked Lists

    160. Intersection of Two Linked Lists Easy Write a program to find the node at which the intersectio ...

  7. LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)

    160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...

  8. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  9. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

随机推荐

  1. 餐厅到店点餐系统app燃尽图

    队友: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  2. LIS(nlogn) POJ 3903 Stock Exchange

    题目传送门 题意:LIS最长递增子序列 O(nlogn) 分析:设当前最长递增子序列为len,考虑元素a[i]; 若d[len]<a[i],则len++,并使d[len]=a[i]; 否则,在d ...

  3. [iOS] UIImage和CGImageRef

    CGImageRef并不是面向对象的API,也不是类,只是一个指针类型,Quartz 2D对CGImageRef的定义为: typedef struct CGImage *CGImageRef; 由此 ...

  4. Cite a Website in Paper 论文中引用网页的格式

    Template: 1.A. Author Surname, 'Title', Year Published, <http://Website-Url> (accessed 10 Octo ...

  5. 30个非常流行的提示信息插件(jQuery Tooltip Plugin)

    在网站的设计中,提示信息是非常细微的功能,但是起着非常重要的作用.如果你的网站中提示信息做的比较好,会给浏览者留下非常深刻的印象,同时也会起到非常好的网站宣传效果,下面介绍了30个比较流行提示信息插件 ...

  6. 浅谈Java中的Set、List、Map的区别

    http://developer.51cto.com/art/201309/410205_all.htm

  7. mysql时该如何估算内存的消耗,公式如何计算?

    经常有人问配置mysql时该如何估算内存的消耗.那么该使用什么公式来计算呢? 关心内存怎么使用的原因是可以理解的.如果配置mysql服务器使用太少的内存会导致性能不是最优的;如果配置了太多的内存则会导 ...

  8. js 表单内容使用ajax以json格式混合提交

    脚本代码 function submitForm(){    var post_data = $("#form1").getdict();    var data_dict = { ...

  9. 10 位顶级 PHP 大师的开发原则

    10 位顶级 PHP 大师的开发原则 ruby_chen 发布于: 2013年03月28日 (61评) 分享到:    收藏 +139 #深圳# 6月10日 亚马逊AWSome Day云计算免费培训报 ...

  10. 撑起大规模PHP网站的开源工具

    撑起大规模PHP网站的开源工具 百万级PHP站点Poppen.de的架构 在 2011年11月27日 那天写的     已经有 3957 次阅读了 感谢 参考或原文   服务器君一共花费了54.510 ...