http://www.geeksforgeeks.org/write-a-function-to-get-the-intersection-point-of-two-linked-lists/

第一第二个方法比较简单,下面这段代码是第三个方法

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node* head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
} int getintersect(node *first, node *second) {
int c1 = ;
int c2 = ;
node *p = first;
while (p) {
c1++;
p = p->next;
}
p = second;
while (p) {
c2++;
p = p->next;
}
if (c1 < c2) {
swap(c1, c2);
swap(first, second);
}
while (c1 != c2) {
c1--;
first = first->next;
}
while (first != second) {
first = first->next;
second = second->next;
}
return second->data;
} int main() {
node *head = new node();
head->next = new node();
head->next->next = new node();
head->next->next->next = new node();
head->next->next->next->next = new node();
node *second = new node();
second->next = head->next->next->next;
cout << getintersect(head, second);
return ;
}

第四第五个方法不太容易想到

Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.的更多相关文章

  1. [Algorithm] Linked List Data Structure in JavaScript

    A linked list is a collection of items where each item points to the next one in the list. Because o ...

  2. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  3. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  4. Leetcode: All O`one Data Structure

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  5. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  6. [leetcode]432. All O`one Data Structure全O(1)数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  7. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  8. 面试总结之数据结构(Data Structure)

    常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...

  9. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

随机推荐

  1. 单机Redis实现分布式互斥锁

    代码地址如下:http://www.demodashi.com/demo/12520.html 0.准备工作 0-1 运行环境 jdk1.8 gradle 一个能支持以上两者的代码编辑器,作者使用的是 ...

  2. Java除法结果带小数、进一法的实现 Java问题通用解决代码

    http://blog.csdn.net/windone0109/article/details/5355379进一法: 即省略的位上只要大于零都要进一位 :  四舍五入法: 即省略的位上小于五都要舍 ...

  3. sublime 插件篇

    添加插件 进入https://packagecontrol.io/installation   找到 或者 直接复制 import urllib.request,os,hashlib; h = '6f ...

  4. laravel模型建立和数据迁移和数据填充(数据填充没有成功)未完

    开始创建我们的第一个 Article 模型及其对应迁移文件了,我们在项目根目录运行如下 Artisan 命令一步到位: php artisan make:model Article -m -m 是 - ...

  5. void 指针的转换

    不论什么类型的指针都能够显式转换为void类型,且不会丢失数据.例如以下面程序: #include<stdio.h> int main(void) { short a=5; void *p ...

  6. wxWidgets之wxGrid控件

    1. 介绍 wxGrid控件时wxWidgets界面库中内置的网格控件. 通经常使用来显示表格数据.该控件拥有强大的功能.开发人员可依据自己的需求对其进行定制. 2. 经常使用API      构造函 ...

  7. SQL之经典语句

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  8. laravel学习之路1:认证相关

    Laravel中Auth::guard()表示什么意思? Auth::check() 是判断用户是否登录的方法,如果使用的默认用户系统,那这样使用没问题. 但是使用两组用户的话,如何使用各组用户的功能 ...

  9. ios NavigationViewController跳转以及返回传值

    (一)使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面.这种话.下一页面相同在NavigationViewContr ...

  10. Android中打包JAR时获取资源ID的方法

    前言:在打包android源码的时,有的时候源码中包含了资源文件,但是jar包中不包含,所以会异常,解决的方案就是不用系统的提供的id名,而是直接 获取id,如反射. 1.系统提供的方法: /** * ...