160. Intersection of Two Linked Lists【easy】

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2

c1 → c2 → c3

B: b1 → b2 → b3

begin to intersect at node c1.

Notes:

    • If the two linked lists have no intersection at all, return null.
    • The linked lists must retain their original structure after the function returns.
    • You may assume there are no cycles anywhere in the entire linked structure.
    • Your code should preferably run in O(n) time and use only O(1) memory.

解法一:

 class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
// write your code here
if(headA == NULL || headB == NULL)
return NULL;
ListNode* iter1 = headA;
ListNode* iter2 = headB;
int len1 = ;
while(iter1->next != NULL)
{
iter1 = iter1->next;
len1 ++;
}
int len2 = ;
while(iter2->next != NULL)
{
iter2 = iter2->next;
len2 ++;
}
if(iter1 != iter2)
return NULL;
if(len1 > len2)
{
for(int i = ; i < len1-len2; i ++)
headA = headA->next;
}
else if(len2 > len1)
{
for(int i = ; i < len2-len1; i ++)
headB = headB->next;
}
while(headA != headB)
{
headA = headA->next;
headB = headB->next;
}
return headA;
}
};

先算长度,然后长的先走差值步,然后同时走

解法二:

 public class Solution {
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
} // get the tail of list A.
ListNode node = headA;
while (node.next != null) {
node = node.next;
}
node.next = headB;
ListNode result = listCycleII(headA);
node.next = null;
return result;
} private ListNode listCycleII(ListNode head) {
ListNode slow = head, fast = head.next; while (slow != fast) {
if (fast == null || fast.next == null) {
return null;
} slow = slow.next;
fast = fast.next.next;
} slow = head;
fast = fast.next;
while (slow != fast) {
slow = slow.next;
fast = fast.next;
} return slow;
}
}

先弄成环,转换为找环的入口问题,找到之后再断开环

找环的问题解法可以参见(142. Linked List Cycle II【easy】

160. Intersection of Two Linked Lists【easy】的更多相关文章

  1. 160. Intersection of Two Linked Lists【Easy】【求两个单链表的第一个交点】

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 380. Intersection of Two Linked Lists【medium】

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  3. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  4. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  5. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  6. 234. Palindrome Linked List【easy】

    234. Palindrome Linked List[easy] Given a singly linked list, determine if it is a palindrome. Follo ...

  7. [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 ...

  8. [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 ...

  9. 【LeetCode】160. Intersection of Two Linked Lists

    题目: Write a program to find the node at which the intersection of two singly linked lists begins. Fo ...

随机推荐

  1. 【贪心】【线性基】bzoj2460 [BeiJing2011]元素 / bzoj3105 [cqoi2013]新Nim游戏

    p2460: #include<cstdio> #include<algorithm> using namespace std; #define N 1001 typedef ...

  2. 【函数式权值分块】【块状链表】bzoj3065 带插入区间K小值

    显然是块状链表的经典题.但是经典做法的复杂度是O(n*sqrt(n)*log^2(n))的,出题人明确说了会卡掉. 于是我们考虑每个块内记录前n个块的权值分块. 查询的时候差分什么的,复杂度就是O(n ...

  3. C语言计算器

    地址:  https://wenda.so.com/q/1371173683061754?src=140

  4. 在xcode6中使用矢量图(iPhone6置配UI)

    转载出处:http://blog.xoneday.com ios应用程序是一个图像主导的产品.在开发一个应用程序时,你需要各种尺寸的图标,你需要为每个图像文件制作一个@1x尺寸和一个@2x尺寸.这样你 ...

  5. Bootstrap标签Tabs

    <!--标签--> <ul class="nav nav-tabs" role="tablist"> <li class=&quo ...

  6. CSS 布局模型

    css布局模型 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之上.又不同于我们常说的 CSS 布局样式或 CSS 布局模板.假设说布局模型是本.那么 CSS ...

  7. Hibernate 参数匹配查询

    第一种: public User validate(String userName, String password) { String hql = "from User where use ...

  8. js 正则只允许小写字母、数字、点、中短划线

    正则表达式如下: /^[a-z0-9\.-]*$/g 可用如下语句验证: alert(/^[a-z0-9\.-]*$/g.test('abc123.45a-b')); //true alert(/^[ ...

  9. [C++]豆知识(1条)

    术语: ctor:constructor,构造函数 dtor:destructor,析构函数 构造函数/析构函数 如果基类要利用多态,则dtor需要声明为virtual,这样在销毁对象时才可以正确调用 ...

  10. 构建高性能web站点-1

    以下为阅读<构建高性能web站点>郭欣 著 这本书的适合读者: 1.编写web程序.关心站点性能,并且希望自己做的更加出色的开发人员 2.关心性能和可用性的web架构师 3.希望构建高性能 ...