160. Intersection of Two Linked Lists(链表)
来源:互联网 发布:微信蓝精灵软件 编辑:程序博客网 时间:2024/05/18 01:31
https://leetcode.com/problems/intersection-of-two-linked-lists/description/
题目:找2个链表的公共节点。
思路:遍历2遍,然后判断。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if(headA==NULL||headB==NULL) return NULL; int len1=0,len2=0; ListNode *a=headA; ListNode *b=headB; while(a!=NULL) { len1++;a=a->next; } while(b!=NULL) { len2++; b=b->next; } a=headA; b=headB; if(len1>=len2) { len1-=len2; while(len1) { a=a->next; len1--; } } else { len2-=len1; while(len2) { b=b->next; len2--; } } while(a!=NULL&&b!=NULL) { if(a==b) return a; a=a->next; b=b->next; } return NULL; }};
阅读全文
0 0
- 160. Intersection of Two Linked Lists(链表)
- [LeetCode]160.Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 【LeetCode】160.Intersection of Two Linked Lists
- [leetcode] 160.Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- [LeetCode]160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode--160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- LeetCode 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- 【LeetCode】160. Intersection of Two Linked Lists
- 160. Intersection of Two Linked Lists
- unicode中的‘\xa0’字符在转换成gbk编码时会出现问题,gbk无法转换'\xa0'字符。
- 不断更新中:中文垃圾邮件过滤规则集Chinese_rules.cf
- JAVA随手笔记一之图标按钮
- 机器学习工程师自学指南
- C++primer第五版第三章答案(2)
- 160. Intersection of Two Linked Lists(链表)
- [js高手之路] 跟GhostWu一起封装一个字符串工具库-架构篇(1)
- 游戏服务器开发都要学什么
- [编程题] 编码
- 【stm32f103】SysTick实现延时(寄存器版)
- 系统中hosts文件的作用和修改(全部)和HTTP 错误 404.0
- 堆和栈的区别
- Xshell 通过跳板机登录服务器
- C++中Json的用法(上)