欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode160——相交链表

程序员文章站 2022-06-17 19:21:52
...

题目: 

LeetCode160——相交链表

思路: 

LeetCode160——相交链表

 代码:

struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
	ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
		if (headA == NULL || headB == NULL)
			return NULL;
		ListNode* p1 = headA;
		ListNode* p2 = headB;

		while (p1!=p2)
		{
			p1 = (p1 == NULL) ? headB : p1->next;
			p2 = (p2 == NULL) ? headA : p2->next;

		}
		return p1;

	}



};

 

相关标签: LeetCode