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

#include 
#include 
#include 

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;
}

你可能感兴趣的:(数据结构)