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. Hibernate核心类和接口具体介绍

    一.hiobernate核心类和接口预览图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveGxnZW4xNTczODc=/font/5a6L5L2T/fo ...

  2. 操作符(运算符)重载 或者叫 二元运算符 operator + 与 转换式操作符 implicit operator explicit operator

    static void Main(string[] args) { rational r1 = new rational(5); rational r2 = new rational(51); rat ...

  3. 闪屏(Splash)

    好久没弄ReactNative了, 写个怎样实现闪屏(Splash)的文章吧. 注意: (1) 怎样切换页面. (2) 怎样使用计时器TimerMixin. (3) 怎样使用动画效果. (4) 怎样载 ...

  4. bean对应mapper.xml字段

    在bean中set的时候最好写上这个,避免报空指针............... public void setImgAddress(String imgAddress) { this.imgAddr ...

  5. C#中使用泛型对照使用通用基础类型效率减少近一倍

     C#中使用泛型对照使用通用基础类型效率减少近一倍 以下是測试结果: CSharp class and generic TotalMilliseconds: 270772.9229CSharp g ...

  6. SQL 时间格式转换

    ------- 获取当前时间 -------- DECLARE @currentTime varchar(); SET @currentTime = CONVERT(VARCHAR(),GETDATE ...

  7. Unity3D 之IAP

    本人是一个Unity忠实爱好者,鉴于网上关于Unity的内置付费教程 少之甚少,本人就把自己倒腾过的IAp分享出来,仅供大家参考.一.搭建号沙盒环境( 详细请看:http://xiaominghimi ...

  8. php获取post内容方式

    PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型. php获取post参数的几种方式 1.$_POST['paramName'] 只能接收Co ...

  9. iOS scrollView中嵌套多个tableView处理方案

    项目中经常会有这样的需求,scrollView有个头部,当scrollView滚动的时候头部也跟着滚动,同时头部还有一个tab会锁定在某个位置,scrollView中可以放很多不同的view,这些vi ...

  10. swift - 实现类似今日头条顶部标签和底部内容的动态解决方案

    TYPageView TYPageView 类似今日头条 的标签导航解决方案,支持多种样式选择,基于swift3.0,支持文字颜色动态变化,底部选中线的动态变化 配图: 使用方法: let title ...