Intersection of Two Linked Lists
来源:互联网 发布:如何打开端口8081 编辑:程序博客网 时间:2024/04/30 12:48
/** * 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; ListNode *node1=headA; int length1=0; while(node1!=NULL) { ++length1; node1=node1->next; } ListNode *node2=headB; int length2=0; while(node2!=NULL) { ++length2; node2=node2->next; } int diff=length1-length2; if(diff<0) { node1=headA; while(-diff) { headB=headB->next; ++diff; } node2=headB; } else { node2=headB; while(diff) { headA=headA->next; --diff; } node1=headA; } while(node1&&node2) { if(node1==node2) return node1; node1=node1->next; node2=node2->next; } return NULL; }};
0 0
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- Intersection of Two Linked Lists
- 旋转的太极
- open()与fopen()的区别
- 三星拍照旋转问题
- Linux 查看进程以及子进程/线程
- 剑指offer 37 - 两个链表的第一个公共节点
- Intersection of Two Linked Lists
- IE乱码火狐不乱码解决
- Spark实战
- 运用栈非递归遍历二叉树
- How do I add classes to main menu ul and li in Drupal 8
- mongrel
- Hibernate
- RabbitMq消息序列化简述
- MyBatis 之ResultMap 使用介绍