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 ...
随机推荐
- BZOJ1103 [POI2007]大都市meg(DFS序)
题目:一颗树,单边修改,链上查询..实际上链是根到结点的链.网上好像有其他做法,我的想法是这样的: 先不看修改,毫无疑问查询只是查询结点的深度:而修改一条边会有什么影响:影响是且只是以边上深度最深结点 ...
- HDU2841 Visible Trees(容斥原理)
题目..大概就是有个m*n个点的矩形从(1,1)到(m,n),问从(0,0)出发直线看过去最多能看到几个点. 如果(0,0)->(x,y)和(0,0)->(x',y')两个向量平行,那后面 ...
- ural 1269. Obscene Words Filter
1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check mess ...
- 51nod 1165 整边直角三角形的数量
1165 整边直角三角形的数量 直角三角形,三条边的长度都是整数.给出周长N,求符合条件的三角形数量. 例如:N = 120,共有3种不同的满足条件的直角3角行.分别是:{20,48,52}, {24 ...
- jquery 获得多选框的值
$('input[name="aihao"]:checked').each(function(){ str +=$(this).val()+','; selecthtml +='& ...
- JAVA生成验证码
<img border="0" src="ValidateCode" ...
- hdu Load Balancing
这道题题目表示看不懂,如果哪位明白题意的,还望在评论里留个言指导一下!
- 利用onekeyup即可实现验证码的点击刷新功能
显示验证码 首先在Home/Controller下创建一个公共控制器PublicController 1 <php2 namespace Home\Controller;3 4 use Thin ...
- http页面转发和重定向的区别
一.调用方式 我们知道,在servlet中调用转发.重定向的语句如下:request.getRequestDispatcher("new.jsp").forward(request ...
- T-SQL查询进阶--深入浅出视图
视图可以看作定义在SQL Server上的虚拟表.视图正如其名字的含义一样,是另一种查看数据的入口.常规视图本身并不存储实际的数据,而仅仅存储一个Select语句和所涉及表的metadata. 视图简 ...