/*************************************************************************
> File Name: 35_FirstCommonNode.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年09月02日 星期五 20时52分28秒
************************************************************************/ #include <stdio.h>
#include <malloc.h> // 链表结构体
struct ListNode
{
int val;
struct ListNode* next;
}; // 顺序输出链表
void PrintList(ListNode* head)
{
if (head == NULL)
return;
ListNode* temp = head;
printf("PrintList:\n");
while (temp != NULL)
{
printf("%d ", temp->val);
temp = temp->next;
}
printf("\n");
} // 获取链表长度
int getListLength(ListNode* head)
{
int length = ;
ListNode* p = head;
while (p)
{
length ++;
p = p->next;
}
return length;
} // 寻找第一个公共结点
ListNode* FindFirstCommonNode(ListNode* head1, ListNode* head2)
{
int length1 = getListLength(head1);
int length2 = getListLength(head2); ListNode* longList;
ListNode* shortList;
int diff; if (length1 >= length2)
{
longList = head1;
shortList = head2;
diff = length1 - length2;
}
else
{
longList = head2;
shortList = head1;
diff = length2 - length1;
} for (int i = ; i < diff; ++i)
longList = longList->next; while (longList && shortList && shortList!=longList)
{
longList = longList->next;
shortList = shortList->next;
} if (longList == NULL)
printf("Not Find\n");
else
printf("Find %d\n", longList->val); return longList;
} int main()
{
// 测试链表结构
ListNode* head1 = (ListNode*)malloc(sizeof(ListNode));
head1->val = ;
ListNode* p1 = head1;
for (int i = ; i < ; ++i)
{
ListNode* q1 = (ListNode*)malloc(sizeof(ListNode));
q1->val = i + ;
p1->next = q1;
p1 = q1;
}
p1->next = NULL; ListNode* head2 = (ListNode*)malloc(sizeof(ListNode));
head2->val = ;
ListNode* p2 = head2;
for (int i = ; i < ; ++i)
{
ListNode* q2 = (ListNode*)malloc(sizeof(ListNode));
q2->val = i + ;
p2->next = q2;
p2 = q2;
}
p2->next = NULL; p1->next = head2->next->next; PrintList(head1);
PrintList(head2); FindFirstCommonNode(head1, head2);
}

剑指Offer35 两个链表第一个公共结点的更多相关文章

  1. 剑指Offer - 两个链表第一个公共节点

    https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=11189&tPage= ...

  2. 剑指Offer-35.两个链表的第一个公共结点(C++/Java)

    题目: 输入两个链表,找出它们的第一个公共结点. 分析: 先统计两个链表的长度,计算他们的差值,然后将两个链表对齐,再去寻找公共节点即可. 程序: C++ class Solution { publi ...

  3. 【剑指Offer】删除链表中重复的结点 解题报告(Python)

    [剑指Offer]删除链表中重复的结点 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interview ...

  4. 剑指 Offer 52. 两个链表的第一个公共节点 + 链表 + 第一个公共结点 + 双指针

    剑指 Offer 52. 两个链表的第一个公共节点 Offer_52 题目详情 题解分析 可以使用两个指针 node1,node2 分别指向两个链表 headA,headB 的头结点,然后同时分别逐结 ...

  5. 剑指Offer 两个链表的第一个公共结点

    题目描述 输入两个链表,找出它们的第一个公共结点.   思路: 题目说的很笼统,应该是有2个链表,找出公共点,第一个公共点后面的链表是共同所有的.可以用map做,直接检测map里有没有出现这个节点. ...

  6. 剑指Offer——两个链表的第一个公共结点

    题目描述: 输入两个链表,找出它们的第一个公共结点. 分析: 设置两个指针,分别从两个链表的头部开始往后遍历. 谁遍历完自己本身的,就从另一个链表开始遍历,这样大家到达第一个公共结点的时候便会相遇. ...

  7. 剑指offer--44.两个链表的第一个公共结点

    @selfboot 牛逼的代码,长度相同,一遍出结果, 长度不同,短的点跑完,变成长的,当长的跑完变成短的链表的时候,较长的链表已经走过了多的结点. ------------------------- ...

  8. 用js刷剑指offer(两个链表的第一个公共结点)

    题目描述 输入两个链表,找出它们的第一个公共结点. 牛客网链接 js代码 /*function ListNode(x){ this.val = x; this.next = null; }*/ fun ...

  9. 【Java】 剑指offer(18) 删除链表中重复的结点

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重 ...

随机推荐

  1. KMP算法及java实现

    参考: http://blog.csdn.net/cdnight/article/details/11935387

  2. Animation Spinner【项目】

    https://github.com/vjpr/healthkick/blob/master/src/win/healthkick/ucSpinnerCogs.xaml 网上的例子,放在UserCon ...

  3. typeof与GetType区别及反射的见解

    http://www.cnblogs.com/knowledgesea/archive/2013/03/02/2935920.html http://www.cnblogs.com/Jax/archi ...

  4. Why isn't there a SendThreadMessage function?

    Here's an interesting customer question: Windows has PostMessage and SendMessage. It also has PostTh ...

  5. C166 Interfacing C to Assembler

    Interfacing C to Assembler You can easily interface your C programs to routines written in XC16x/C16 ...

  6. C语言中结构体 自引用 和 相互引用

    http://blog.163.com/modingfa_002/blog/static/11092546620133193264579 结构体的自引用(self reference),就是在结构体内 ...

  7. C++异常

    相对于C语言,C++增加了异常机制.考虑,异常解决了什么问题,又带来了什么问题. 异常解决了什么问题: 1.问题检测与问题处理相分离. 2.C语言只是返回一个整数,而异常带有上下文信息,方便找出问题. ...

  8. mongodb - 命令行增删改查

    # insert db.person.insert({ }) db.person.insert({ }) db.person.insert({ }) db.person.insert({ }) # s ...

  9. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题

    B. Guess the Permutation 题目连接: http://www.codeforces.com/contest/618/problem/B Description Bob has a ...

  10. unity3d脚本编程

    一 创建和使用脚本 1 概述 GameObject的行为都是被附加到其上面的组件控制,脚本本质上也是一个组件. 在unity中创建一个脚本,默认内容例如以下: using UnityEngine; u ...