[LeedCode OJ]#160 Intersection of Two Linked Lists

【 声明:版权所有,转载请标明出处,请勿用于商业用途。  联系信箱:[email protected]】

题目链接:https://leetcode.com/problems/intersection-of-two-linked-lists/

题意:

给定两个链表,要求找出这两个链表的交点

思路:

我们可以设定两个指针,分别遍历得到a,b的长度,然后如果a长,就将a的指针从头指针往下移动k位直到与b指针等长的位置,b链表长也是如此。

然后两个指针同时出发,一旦走到相同的位置,那么这个位置就是两个链表的交点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution
{
	public:
		ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
		{
			int la = 0,lb = 0;
			ListNode *pa = headA;
			ListNode *pb = headB;
			while(pa)
			{
				la++;
				pa = pa->next;
			}
			while(pb)
			{
				lb++;
				pb = pb->next;
			}
			pa = headA;
			pb = headB;
			if(la<lb)
			{
				int cnt = lb - la;
				while(cnt--)
				{
					pb = pb->next;
				}
			}
			else if(la>lb)
			{
				int cnt = la - lb;
				while(cnt--)
				{
					pa = pa->next;
				}
			}
			while(pa!=pb)
			{
				pa = pa->next;
				pb = pb->next;
			}
			return pa;
		}
};

版权声明:本文为博主原创文章,如果转载,请注明出处

时间: 2024-10-10 21:36:31

[LeedCode OJ]#160 Intersection of Two Linked Lists的相关文章

LeetCode OJ 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

160. Intersection of Two Linked Lists(js)

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: begin to intersect at node c1. Example 1: Input: intersectVal = 8, listA

[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

[leedcode 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 OJ: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. 找出第一个合并的节点的位置(或者说插入),注意复杂度为O(N),那么先遍历两个链

[LeetCode 160] 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

leetcode 160. Intersection of Two Linked Lists --------- 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. Notes: If the two linked lists have no i

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 example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. 解题思路: 先遍历两个list的长度,然后长的减去短的,之后同时遍历即可,JAV