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

继而,相继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. 通过程序校验xml文档学习笔记

    校验xml文档,可以通过程序来校验,利用一段js代码即可. 各行代码的含义已经写出,运行这个html文件,检验如下xml代码: 结果如下: 如果xml文档出现错误: 结果如下: 其中,obj.asyn ...

  2. Redis Cluster 3.0搭建与使用

    Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施( ...

  3. Powershell变量的类型

    Powershell 默认支持的.NET类型如下:   [order], [pscustomobject], [array], [bool], [byte], [char], [datetime], ...

  4. memcached与spring

    key的生成规则 update 与 query 的参数不一样,如何让其生成一样的key 列表缓存如何定义key及失效 最近同事推荐了一个开源项目:Simple-Spring-Memcached(简称s ...

  5. 校内OJ 1128 词链(link)(Trie+DFS)

    1128: 词链(link) 时间限制: 1 Sec  内存限制: 64 MB 提交: 23  解决: 7 [提交][状态][讨论版] 题目描述 给定一个仅包含小写字母的英文单词表,其中每个单词最多包 ...

  6. html5shiv.js-让IE浏览器支持HTML5标准

    兼容性IE8及以下IE版本 浏览器IE8及以下IE版本对HTML5标签的支持是有限的,我们可以通过在网页中添加脚本的方式来解决目前IE浏览器对HTML5支持的问题. <!–[if IE]> ...

  7. 使用Android Studio和Genymotion模拟器搭建Andriod开发环境

    一.Android Studio下载 1.打开http://www.android.com/ 2.依照下图步骤打开下载页面 a.在页脚部分点击“App Developer Resources” b.点 ...

  8. snowflake

    snowflake在分布式系统中生成全局id

  9. Javascript 笔记与总结(2-18)正则验证与正则匹配

    ① 判断 String 是否符合正则要求 patt.test(String); [例]表单提交: a.用户名不能为空,只能是数字及字母,6-11位 b.email 不能为空且格式正确 <!DOC ...

  10. Overengineering

    https://en.wikipedia.org/wiki/Overengineering Overengineering (or over-engineering) is the designing ...