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 ...
随机推荐
- 教你如何把php项目打包成EXE文件发布
家经常会接到一些编程的活,例如设计企业网站,做做财务,统计系统什么的.或许是因为朋友的需求,或许图个零花.不管什么原因吧.等程序做好了,给对方展示.安装,就成了问题.企业网站好说,至少需要个虚拟主机什 ...
- Python修改文件权限
os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...
- quartz项目中的运用
下面是之前项目中quartz的运用,我将它梳理出来. 测试类: public class OrdExpireTaskMain { public static void main(String[] ar ...
- 跳转 nginx 跳转 apache跳转
公司在google上投广告,需要做一些很简单的站去google上投广告,当用户在google上点击那些很简单的网站的时候,就会跳转到真实的网站.但是,如果用户直接在浏览器输入域名,并访问的话,那样就不 ...
- zabbix 自定义脚本监控activemq
1. 编写获取activemq队列积压消息(check-amq.sh) #!/bin/bash QUEUENAME=$ MQ_IP='172.16.1.56' curl -uadmin:admin h ...
- 将Xcode升级到10.0以上版本,Appium启动报错的问题
前言 现在的Xcode最新版本都是在10.1,原先使用的版本是9.4.1!结果今天手贱将其升级... 然后,跑IOS自动化时,出现“Xcode version '0.1'. Support for X ...
- 如何在github上发起一个pull request,如何贡献代码,参与开源项目
点击页面右上角的 “fork” ,把你关注的项目fork到你自己的账号下了. 把项目克隆到本地 修改并push 回到你的github界面,发起请求: 在自己fork的库处新建请求:New pull r ...
- Android使用JUnit进行单元测试
前言:为什么要进行单元测试?单元测试能快速是开发者,找到代码中的问题所在,因为是单元测试,所以代码只执行响应的测试单元,执行快解决问题的效率高,同时提高代码的质量. Android中的单元测试可简单分 ...
- Domino Angular 前端UI开发
因为如今前端的要求越来越专业化,不少企业已经有前端的专业职位了.当然我们dominio软件企业.有些也在特意招一些前端的project师. 比方如今流程的多平台(之前我的有教程).就必需要有专业的UI ...
- 九度OJ 1343:城际公路网 (最小生成树)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:445 解决:178 题目描述: 为了加快城市之间的通行和物资流动速度,A国政府决定在其境内的N个大中型城市之间,增加修建K条公路.已知这N个 ...