交叉链表 C++ 返回交叉节点

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct ListNode
{
	int val;
	ListNode* next;
	ListNode(int value, ListNode* n) : val(value), next(n)
	{

	}
};

class List{
public:
	ListNode* head;
	ListNode* tail;
	int size;

	List()
	{
		head = nullptr;
		tail = nullptr;
		size = 0;
	}

	ListNode* get(int index)
	{
		int temp = 0;
		ListNode* cur = head;
		while (cur != nullptr)
		{
			if (temp == index)
			{
				return cur;
			}
			cur = cur->next;
			temp++;
		}
		return nullptr;
	}

	void addAtHead(int value)
	{
		if (head != nullptr)
		{
			ListNode* newNode = new ListNode(value, head);
			head = newNode;
		}
		else
		{
			head = new ListNode(value, nullptr);
			tail = head;
		}
		size++;
	}

	void addAtTail(int value)
	{
		if (tail != nullptr)
		{
			ListNode* newNode = new ListNode(value, nullptr);
			tail->next = newNode;
			tail = newNode;
		}
		else
		{
			tail = new ListNode(value, nullptr);
			head = tail;
		}
		size++;
	}

	void addAtIndex(int index, int value)
	{
		if (index <= 0)
		{
			addAtHead(value);
			return;
		}
		if (index == size)
		{
			addAtTail(value);
			return;
		}
		int temp = 0;
		ListNode* pre = nullptr;
		ListNode* cur = head;
		while (cur != nullptr)
		{
			if (temp == index)
			{
				ListNode* newNode = new ListNode(value, cur);
				if (pre != nullptr)
				{
					pre->next = newNode;
				}
				size++;
				return;
			}
			pre = cur;
			cur = cur->next;
			temp++;
		}
	}

	void deleteLast()
	{
		int temp = 0;
		ListNode* pre = nullptr;
		ListNode* cur = head;
		while (cur != nullptr)
		{
			pre = cur;
			cur = cur->next;
			temp++;
		}
		ListNode* oldNode = cur;
		tail = pre;
		delete oldNode;
		size--;
	}

	void deleteAtIndex(int index)
	{
		if (index == 0)
		{
			ListNode* oldNode = head;
			head = head->next;
			delete(oldNode);
			size--;
			return;
		}
		if (index == size-1)
		{
			deleteLast();
		}
		int temp = 0;
		ListNode* pre = nullptr;
		ListNode* cur = head;
		while (cur != nullptr)
		{
			if (temp == index)
			{
				ListNode* oldNode = cur;
				if (pre != nullptr)
				{
					pre->next = cur->next;
				}
				delete oldNode;
				size--;
				return;
			}
			pre = cur;
			cur = cur->next;
			temp++;
		}
	}
};

class Solution{
public:
	ListNode* getIntersectionNode(ListNode* a, ListNode* b)
	{
		if (a == nullptr || b == nullptr)
		{
			return nullptr;
		}
		ListNode* pA = a;
		ListNode* pB = b;
		while (pA != pB)
		{
			pA = pA == nullptr ? b : pA->next;
			pB = pB == nullptr ? a : pB->next;
		}
		return pA;
	}
};

int main()
{
	ListNode* node1 = new ListNode(1, nullptr);
	ListNode* node2 = new ListNode(2, nullptr);
	ListNode* node3 = new ListNode(3, nullptr);
	ListNode* node4 = new ListNode(4, nullptr);
	ListNode* node5 = new ListNode(5, nullptr);
	ListNode* node6 = new ListNode(6, nullptr);
	ListNode* node7 = new ListNode(7, nullptr);
	ListNode* node8 = new ListNode(8, nullptr);

	node1->next = node2;
	node2->next = node3;
	node3->next = node4;
	node4->next = node5;

	node6->next = node7;
	node7->next = node8;
	node8->next = node3;

	Solution* s = new Solution();
	ListNode* result;
	result = s->getIntersectionNode(node1, node6);

	printf("%d\n", result->val);

	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值