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

继而,相继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. 关于 Apple Metal API 的一些想法

    在看完 Metal 的开发文档后,除了官方所宣称的一些优点外(比如说更容易理解和使用的 API,更直接和精细的硬件控制,减少 GPU 使用过程中的 CPU 额外开销等等),从我有限的 GLES 开发经 ...

  2. 【SPOJ】1825. Free tour II(点分治)

    http://www.spoj.com/problems/FTOUR2/ 先前看了一会题解就自己yy出来了...对拍过后交tle.................. 自己造了下大数据........t ...

  3. C#SortedList排序列表怎么样逆序输出

    C#分别在集合库和泛型库中有共2套SortedList 以较新的泛型库为例,SortedList<int, string> l = new SortedList<int, strin ...

  4. Delphi 复习代码

    1.取得可文件路径 Path := ExtractFilePath(Application.ExeName); //取得可执行文件路径 TXMLDocument.Create(ExtractFileP ...

  5. 关于javascript在作用域中的变量定义你所不知道的一些东西

    //先看一段代码 var a = 100; function test(){ alert(a); var a = 200; } test(); /* 结果:undifined 原因:js引擎在执行sc ...

  6. android之TabHost(上)

    首先建立文件res/layout/tab.xml 代码如下: <?xml version="1.0" encoding="utf-8"?> < ...

  7. 使用SimpleXML应该注意的问题有哪些?

    SimpleXML提供了一套简单快速的XML操作方法,大大地提高了XML操作的效率. 但是有时不小心也会带来不小的麻烦,看下面一段代码: $xml=simplexml_load_string('< ...

  8. 滴答数必须介于 DateTime.MinValue.Ticks 和 DateTime.MaxValue.Ticks 之

    一个莫名其妙的问题:错误 滴答数必须介于 DateTime.MinValue.Ticks 和 DateTime.MaxValue.Ticks 之间. 参数名:ticks.这 网上找了很多,都没有一个正 ...

  9. Network | TCP congestion control

    拥塞控制算法:1. 加性增.乘性减:2. 慢启动:3. 对超时事件作出反应: 整体过程如下: 慢启动->到达阈值->加性增(窗口+1个MSS), 这个阶段叫拥塞避免(CA)->3个冗 ...

  10. 【产品更新】EVC8013 硬件更新!

    EVC8013 三合一磁耦合隔离转换器(USB转RS-232 / RS-485 / RS-422 ),迎来一次硬件大幅度升级,升级不加价! 本次升级主要有以下几个方面: 1.采用第二代金升阳 3000 ...