Intersection of Two Linked Lists | LeetCode
利用两个栈,然后分别存储每一个链表。
继而,相继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的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- 2016.5.24——Intersection of Two Linked Lists
Intersection of Two Linked Lists 本题收获: 1.链表的输入输出 2.交叉链表:这个链表可以有交叉点,只要前一个节点的的->next相同即可. 题目:Inters ...
- 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 ...
- LeetCode--LinkedList--160. Intersection of Two Linked Lists(Easy)
160. Intersection of Two Linked Lists(Easy) 题目地址https://leetcode.com/problems/intersection-of-two-li ...
- 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 ...
- [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 ...
随机推荐
- zoj 1097 普吕弗序列
题目大意:输入一颗无根树的括号序列,求这棵树的普吕弗序列. 分析思路: 1)普吕弗序列,可以参考维基百科,其做法是找出树中编号最小的叶子节点,并将此叶子节点及边删除,并输出其邻接的节点标号: 2)递归 ...
- extjs grid
Ext.onReady(function() { Ext.BLANK_IMAGE_URL = '../resources/images/default/s.gif'; Ext.QuickTips.in ...
- BZOJ2883 : gss2加强版
首先离散化颜色 设pre[x]表示与x颜色相同的点上一次出现的位置,对于每种颜色开一个set维护 修改时需要修改x.x修改前的后继.x修改后的后继 询问[l,r]等价于询问[l,r]内pre[x]&l ...
- 点击repeater的一个修改事件触发全部repeater每一行的修改事件
<td align="center"> <asp:LinkButton ID ="btnvip&q ...
- JAVA排序算法
]; ; i < ; i++){ sort[i] = ran.nextInt(); } System.out.print(;i<sort.length;i++){ ;j<sort ...
- js 字符串中取得第一个字符和最后一个字符
var str = "Hello World";// 删除第一个字符 H,结果为 ello World alert(str.slice(1));// 删除最后一个字符 d,结果为 ...
- Xcode4 使用技巧
Xcode4 使用技巧 使用 xcode4 也有一段时间了,今天整理了一下 xcode4 的一些使用技巧,在这里分享给大家. 设置作者 这里所指的作者就是每个源文件头部注释中的 "Creat ...
- lintcode :Coins in Line II 硬币排成线 II
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是 ...
- node.js 资料
supervison可以调试nodejs,并能自动布署 键入命令: npm -g install supervisor
- LightOJ 1188 Fast Queries(简单莫队)
1188 - Fast Queries PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 64 MB Gi ...