首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >链表相交

链表相交

作者头像
九州暮云
发布2019-08-21 11:34:35
3760
发布2019-08-21 11:34:35
举报
文章被收录于专栏:九州牧云九州牧云

目标

找到两个链表相交的起始节点,交点表示一个链表的结尾与另一个链表中的某个节点链接,形成Y形。如图所示:

输入图片说明
输入图片说明

具体实现

  1. 计算出两个链表的长度:a_len和b_len
  2. 再计算出两个链表长度的差:len­Diff = (a_len ~ b_len)
  3. 通过lenDiff遍历较长的链表
  4. 同时遍历这两个链表,判断是否有相同的节点node,如果有这个节点就返回,没有就返回null

时间复杂度O(n),空间复杂度O(1)。

实现代码:

public class FindIntersectionOfLinkedLists {

	public LinkedListIntersection a;
	public LinkedListIntersection b;
	public void createLists(){
		a = new LinkedListIntersection();
			a.addAtEnd(1);
			a.addAtEnd(10);
			a.addAtEnd(20);
			Node tmp = a.addAtEnd(30);
			a.addAtEnd(40);
			a.addAtEnd(50);
			a.addAtEnd(60);
			System.out.print("List A : ");
			a.display();
		b = new LinkedListIntersection();
			b.addAtEnd(5);
			b.addAtEnd(15);
			b.createIntersection(a,tmp);
			System.out.print("List B : ");
			b.display();
	}
	public void findIntersectionByLength(){
		int a_len=0;
		int b_len=0;
		int lenDiff=0;
		boolean intsctFound = false;
		Node an = a.head;
		Node bn = b.head;
		//计算两个链表的长度:a_len和b_len
		while(an!=null){
			an=an.next;
			a_len++;
		}
		while(bn!=null){
			bn=bn.next;
			b_len++;
		}
		
		// 计算a、b两个链表的长度之差,用长链表减去短链表,根据差值遍历最长的链表,移动最长链表的节点指针,最终的结果会使两个链表长度相等
		an = a.head;
		bn = b.head;
		if(a_len>b_len){
			lenDiff = a_len-b_len;
		//	System.out.print("length diff " +lenDiff );
			while(lenDiff!=0){
				an = an.next;
				lenDiff--;
			}
		}else{
				lenDiff = b_len-a_len;
			while(lenDiff!=0){
				bn = bn.next;
				lenDiff--;
			}
		}
		
		// 同时遍历两个链表,找出交点
		while(an!=null && bn!=null){
			//System.out.print(an.data + " " + bn.data);
			if(an==bn) {
				System.out.print("交点是 " + an.data);
				intsctFound = true;
				break;
			}
			else{
				an = an.next;	
				bn = bn.next;
			}
		}
		if(!intsctFound){
			System.out.print("没有找到交点");
		}
	}
	
	public static void main (String[] args) throws java.lang.Exception
	{
		FindIntersectionOfLinkedLists i = new FindIntersectionOfLinkedLists();
		i.createLists();
		i.findIntersectionByLength();
	}
}
class Node{
	public int data;
	public Node next;
	public Node(int data){
		this.data = data;
		this.next = null;
	}
}
class LinkedListIntersection{
	public Node head;
	public LinkedListIntersection(){
		head=null;
	}
	
	public Node addAtEnd(int data){
		Node n = new Node(data);
		
		if (head==null){
			n.next = head;
			head = n;
		}
		else{
			Node currNode = head;
			while(currNode.next!=null){
				//System.out.print("---->" + currNode.data);
				currNode = currNode.next;
			}
			currNode.next = n;
		}
		return n;
	}
	public void createIntersection(LinkedListIntersection a, Node nd){
		Node hd = a.head; 
		while(hd!=nd){
			hd = hd.next;
		}
		Node currNode = head;
			while(currNode.next!=null){
				currNode = currNode.next;
			}
			currNode.next = hd; ;
	}
	public void display(){
		System.out.println("");
		Node currNode = head;
		while(currNode!=null){
			System.out.print("->" + currNode.data);
			currNode=currNode.next;
		}
		System.out.println("");
	}
}

编译自:Find Intersection Point in Two Linked List

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目标
  • 具体实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档