Easy-题目38:160. Intersection of Two Linked Lists

原创 2016年05月30日 20:28:45

题目原文:
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.
题目大意:
求两个单链表的交点。
题目分析:
考研数据结构复习的时候见过此题,应该是链表处理的题目中比较经典的一道。解法是求出两个链表的长度,然后两个指针分别指向两个链表的头结点,先让较长的链表的指针走过差值的步数,这样两个指针距离尾节点的距离就相同了,让两个节点并行走直到遇到交点。
源码:(language:c)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    if(!headA||!headB)
        return NULL;
    struct ListNode *p;
    int lengtha,lengthb;
    for(p=headA,lengtha=0;p;p=p->next,lengtha++);
    for(p=headB,lengthb=0;p;p=p->next,lengthb++);

    if(lengtha>lengthb) {
        for(int i=0;i<lengtha-lengthb;i++)
            headA=headA->next;
    }
    else if (lengthb>lengtha) {
        for(int i=0;i<lengthb-lengtha;i++)
            headB=headB->next;
    }
    while(headA!=headB) {
        headA=headA->next;
        headB=headB->next;
    }
    return headA;
}

成绩:
32ms,beats 18.75%,众数32ms,78.91%
cmershen的碎碎念:
这道题比较经典,但如果不给出这个算法,自己是很难想到的。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

160. Intersection of Two Linked Lists(Linked List-Easy)

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

leetcode-160-Intersection of Two Linked Lists

Intersection of Two Linked Lists Write a program to find the node at which the intersection of two...

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

160. Intersection of Two Linked Lists

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

[LeetCode]problem 160. Intersection of Two Linked Lists

TAG求两个无环链表第一个相交节点 题目链接方法MD智障啊。真是不服不行。编程之美上有一个基础的问题,只判断是否相交;然后后续有个问题就是如果需要找第一个相交节点该怎么办?当时我随便一想,觉得应该没有...

[LeetCode][160][Intersection of Two Linked Lists ]

题目链接:https://leetcode.com/problems/intersection-of-two-linked-lists/ 题目描述: Write a program to fin...

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

Leetcode 160. Intersection of Two Linked Lists

160. Intersection of Two Linked Lists Total Accepted: 69072 Total Submissions: 228648 Difficulty:...

160 Intersection of Two Linked Lists 求两个链表的交点

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

LeetCode OJ 160. Intersection of Two Linked Lists

LeetCode OJ 160. Intersection of Two Linked ListsDescriptionWrite a program to find the node at whic...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)