【Leetcode】Intersection of Two Linked Lists in JAVA

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.

还是倒着想,把listnode放进栈里,然后一个一个pop出来,直到不一样的话,那么前一个点为合并点。

网上都没查到还有别的写这道题的,看来我是第一波写这个题的了~

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

		if(headA==null||headB==null)	return null;
        Stack<ListNode>	stackA = new Stack<ListNode>();
        Stack<ListNode>	stackB = new Stack<ListNode>();
	Stack<ListNode>	stackC = new Stack<ListNode>();//为了最后输出的合并listnode
        ListNode a = headA;
        ListNode b = headB;
        int i=1,j=1;
		while(a.next!=null)	{
			stackA.push(a);
			a=a.next;
			i++;
		}
		stackA.push(a);//到此为止,把所有的listnode push进stackA,如是下面stackB
		while(b.next!=null){
			stackB.push(b);
			b=b.next;
			j++;
		}
		stackB.push(b);//同上
		if(a.val!=b.val)	return null;

		while(i>0&&j>0){
			ListNode bottomA=stackA.pop();
			ListNode bottomB=stackB.pop();
			if(bottomB.val==bottomA.val)	//如果a和b的stack pop出来一样,存进C里,不一样的话那就输出
				{
				stackC.push(bottomA);
				i--;
				j--;
				}
			else	{
				return stackC.pop();
			}
		}
		if(i==0)	return headA;//如果某一个已经到头了,那么就是head为合并点
		else return headB;
    }
时间: 2024-10-13 23:28:56

【Leetcode】Intersection of Two Linked Lists in JAVA的相关文章

【leetcode】Intersection of Two Linked Lists

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. Notes:

【leetcode】Intersection of Two Linked Lists(easy)

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. Notes: If the two linked lists have no i

LeetCode【160】Intersection of Two Linked Lists

For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 思路比较清晰,首先确定二者的长度并计算长度之差的绝对值dis,让较长链表先走dis步,然后两个指针一起走.如果两个指针相等,则返回,否则当指针达到NULL时,没有交点. AC代码如下: void getLength(ListNode *head , int& len)

[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 example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

leetCode 160. Intersection of Two Linked Lists 链表

160. 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

【LeetCode每天一题】 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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5],

[leetCode][003] 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. Notes: 1. If the two linke

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 example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[C++]LeetCode: 60 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. Notes: If the two linked lists have