本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41593747

Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗
B:     b1 → b2 → b3

begin to intersect at node c1.

思路:

(1)这道算法题是一道较老的题目。记得在数据结构书的习题中出现过。题目不难,但有几个点需要注意。

(2)需要注意:A:链表为空的判断;B:链表是否带有环的判断;C:两链表长度之差处的起始点的寻找。

(3)首先,对链表为空的判断,为空则返回null;

(4)其次,需设置两个临时变量A和B分别指向两链表的起始位置,分别遍历两链表并求得其长度,如果遍历完成A和B指向的不

是同一节点,则说明链表带有环,返回null;

(5)再次,比较两链表的大小,将较大的链表从其起始位置向后移动到两链表长度之差的位置,这样,两链表就出于同一起始位

置了;

(5)最后,判断起始位置对应节点是否相同,不相同则两节点分别后移,最后返回A或B即为结果。

算法代码实现如下所示(PS:题目要求时间复杂度为O(n),空间复杂度为O(1),大家有更好的算法希望能分享,谢谢):

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
	int lenA = 0;
	int lenB = 0;
	ListNode A = headA;
	ListNode B = headB;

	if (A == null || B == null)
		return null;

	while (A.next != null) {
		lenA++;
		A = A.next;

	}

	while (B.next != null) {
		lenB++;
		B = B.next;
	}

	// 如果有环状的,终点会不一样,返回null
	if (A != B)  return null;

	A = headA;
	B = headB;
	// 找到相差的个数,并移动到相同的地方开始比较
	if (lenA > lenB) {
		for (int i = 0; i < lenA - lenB; i++) {
			A = A.next;
		}
	} else {
		for (int i = 0; i < lenB - lenA; i++) {
			B = B.next;
		}
	}

	// 如果不相等则比较其后续是否相等
	while (A != B) {
		A = A.next;
		B = B.next;
	}
	return A;
}

Leetcode_160_Intersection of Two Linked Lists的更多相关文章

  1. [LeetCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. 【leetcode】Intersection of Two Linked Lists

    题目简述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  3. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  4. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  5. Leetcode 160. Intersection of two linked lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  6. (LinkedList)Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  7. Java for LeetCode 160 Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  8. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  9. LeetCode Intersection of Two Linked Lists

    原题链接在这里:https://leetcode.com/problems/intersection-of-two-linked-lists/ 思路:1. 找到距离各自tail 相同距离的起始List ...

随机推荐

  1. Java Spring boot 2.0 跨域问题

    跨域 一个资源会发起一个跨域HTTP请求(Cross-site HTTP request), 当它请求的一个资源是从一个与它本身提供的第一个资源的不同的域名时 . 比如说,域名A(http://dom ...

  2. CentOS7 YUM 安装NGINX

    1.先添加源: nano /etc/yum.repos.d/nginx.repo 把下边这段代码添加到nginx.repo中去.[nginx] name=nginx repo baseurl=http ...

  3. Node.js ZLIB

    Zlib 稳定性: 3 - 文档 可以通过以下方式访问这个模块: var zlib = require('zlib'); 这个模块提供了对 Gzip/Gunzip, Deflate/Inflate, ...

  4. PHP 完整表单实例

    PHP - 在表单中确保输入值 在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website. 在评论 ...

  5. Jmeter(二)_基础元件

    测试计划(Test Plan) 它用来描述一个测试方案,包含与本次性能测试所有相关的功能.也就说本次测试的所有内容是于基于一个计划的. "函数测试模式"复选框,如果被选择,它会使J ...

  6. mysql5.7在centos上安装的完整教程以及相关的“坑”

    安装前的准备 Step1: 如果你系统已经有mysql,如一般centos自带mysql5.1系列,那么你需要删除它,先检查一下系统是否自带mysql yum list installed | gre ...

  7. log4cxx用环境变量设置输出文件名

    log4cxx用环境变量设置输出文件名(金庆的专栏 2016.12)利用环境变量,可以用同一个log4j.xml来配置多个相似进程,输出日志到不同文件.例如多个BaseApp进程使用同一个BaseAp ...

  8. Common-used commands in Docker

    1. Start running a image in background mode docker run -it -d <image>:<tag> e.g. docker ...

  9. AJAX编程实践

    ---------------------------------------------------------------------------------------------------- ...

  10. VMware中的桥接模式、NAT(网络地址转换模式)、Host-only(主机模式):转自:http://blog.chinaunix.net/uid-11798538-id-3061551.html

    其中VMnet1是虚拟机Host-only模式的网络接口,VMnet8是NAT模式的网络接口,这些后面会详细介绍.在个虚拟交换机,分别是-个虚拟机交换机,而在VMware Workstation 5以 ...