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. 3、Linux内核模块学习

    一.内核模块的学习   内核的整体框架是非常的大,包含的组件也是非常多,如何将需要的组件包含在内核中呢?选择一,就是将所有的组件全部编译进内核,虽然需要的组件都可以使用,但是内核过分庞大,势必带来效率 ...

  2. Android下Sqlite的使用(9.7)

    1 http://blog.csdn.net/liuhe688/article/details/6715983 2 http://www.eoeandroid.com/thread-170715-1- ...

  3. Excel中判断一个表中的某一列的数据在另一列中是否存在

      A B C D 1 10   3 有 2 6   e 无 3 3   6 有 判断c列的值在A列中是否存在(假定C列为需要判断列,A列为目标列) 在D1中输入以下公式,然后下拉公式即可 =IF(C ...

  4. lvs+keepalived+bind实现负载均衡高可用智能dns

    整体架构: 1.IP地址规划: Dns1:172.28.0.54 Dns2:172.28.0.55 Dr服务器主:172.28.0.57 Dr服务器从:172.28.0.67 Vip:172.28.0 ...

  5. java 安装后 不能 java javac 说找不到命令 -bash: javac: command not found

    java 安装后 不能 java javac  说找不到命令 -bash: javac: command not found 不是环境变量的问题, 直接cd到java的目录 也不能执行命令 后来发现是 ...

  6. Android模拟屏幕点击input tap替代解决方案

    动机解释 本来直接使用 adb shell -> input 即可模拟 键盘事件,触屏事件keyevent ,text,tap 但是手上的这台目标Android机4.0.3系统的input只支持 ...

  7. MapReduce源码分析之JobSplitWriter

    JobSplitWriter被作业客户端用于写分片相关文件,包括分片数据文件job.split和分片元数据信息文件job.splitmetainfo.它有两个静态成员变量,如下: // 分片版本,当前 ...

  8. httpd在嵌入式中应用

    在启动脚本合适位置添加: httpd -h /usr/app/www/ 即开始httpd服务,并定位到/usr/app/www/ 注:busybox已支持httpd命令,所以直接用即可. busybo ...

  9. linux设备驱动的分层设计思想--input子系统及RTC

    转自:linux设备驱动的分层设计思想 宋宝华 http://blog.csdn.net/21cnbao/article/details/5615493 1.1 设备驱动核心层和例化 在面向对象的程序 ...

  10. Mysql CAST()函数

      (1).CAST()函数的参数是一个表达式,它包括用AS关键字分隔的源值和目标数据类型.以下例子用于将文本字符串'12'转换为整型: SELECT CAST('12' AS int) (2).返回 ...