Data Structure Linked List: Write a function to get the intersection point of two Linked Lists.
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.的更多相关文章
- [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 ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- [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 ...
- [Algorithm] Trie data structure
For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
随机推荐
- Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding
Convolutional Patch Networks with Spatial Prior for Road Detection and Urban Scene Understanding 深度学 ...
- 【LeetCode-面试算法经典-Java实现】【015-3 Sum(三个数的和)】
[015-3 Sum(三个数的和)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an array S of n integers, are there ...
- zabbix监控sockets连接数
配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加 Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对soc ...
- 测试用例使用传统excel还是思维导图(Xmind、MindManager等)?
一.使用感言 实习时随便使用了word文档编写测试用例,也没有人带.后来第一份正式测试工作,也没有人带测试,那时跟着大众学用思维导图写测试用例,发现思维导图非常灵活.目前使用xmind. 使用思维导图 ...
- Asp.Mvc将生成的视图保存为字符串
public static class ViewExtensions { /// <summary> /// 在控制器内获取指定视图生成后的HTML /// </summary> ...
- oracle数据库权限管理
权限管理: oracle 9里面默认的三个username和password: sys change_on_install //权限最高的管理员 system manager //普通的管理员 sco ...
- android电话状态的监听
电话状态的监听: /*** * 继承PhoneStateListener类,我们能够又一次其内部的各种监听方法 然后通过手机状态改变时,系统自己主动触发这些方法来实现我们想要的功能 */ class ...
- 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- 启动 ./spark-shell 命令报错
当使用./spark-shell 命令报错 Caused by: ERROR XJ040: Failed to start database @476fde05, see the next excep ...
- quartus2 13.0+modelsim联合开发环境搭建(win10)
quartus2用于硬件设计代码的综合,检查是否有语法错误:modelsim用于对硬件设计代码进行仿真,观察波形是否与需求一致,需要编写xxx_tb.v才能仿真 一.quartus2安装见这篇文章ht ...