Fortune left-law basis class3- title 10 print two ordered lists common part

Fortune left-law basis class3- title 10 print two ordered lists common part

1. Topic

[Title] Given two ordered list head pointer head1 and head2, printing is common to both lists.

2. Analysis

Efflux manner similar to a common portion of the printed list ( efflux understand the algorithm click View Scheme 3 ), works as follows: Set two pointers point to the first number list 1 and list 2, the comparison of the number of pointer size
(1) If the list numeric 1, the pointer 1 chain shift, a number of the points;
(2) if the list numeric 2, the linked list pointer 2 after the shift, a number of the points;
(3) If the two numbers are equal, the current number of printouts, and the pointer 2 are moved back one.

3. core code

Establish (1) the list

① composed of a data structure pointer field and a data field

typedef struct ListNode
{
	int val;
	ListNode *next;
}*List;

② head node initialization, pay attention here can not be written ashead1 = NULL

List head1 = (List)malloc(sizeof(ListNode));
	head1->next = NULL;

③ using an initialization tail interpolation, a new node is p1, and the tail node is r, the initial point to the first node, after the initialization of the tail node p1 points to the new node connected to form a linked list, then the current node is set to the end node, to continue a new initialization p1 continuous cycle. Finally, the tail node pointing to empty, linked list initialization is complete.

List r1,r2,p1,p2;
	r1 = head1;
	r2 = head2;

	//初始化head1(1,2,3,4)
	for(int i = 0;i < 4;i++)
	{
		p1 = (List)malloc(sizeof(ListNode));
		p1->val = i + 1;
		r1->next = p1;
		r1 = p1;
	}
	r1->next = NULL;

(2) Process efflux

With the proviso that when one efflux go list header end.
Note that the conditions continue to findhead1 != NULL && head2 != NULL , but can not be writtenhead1->val != NULL && head2 ->val!= NULL

void merge(List head1,List head2)
{
	while(head1 != NULL && head2 != NULL)
	{
		if(head1->val < head2->val)
		{
			head1 = head1->next;
		}
		else if(head1->val > head2->val)
		{
			head2 = head2->next;
		}
		else
		{
			cout<<head1->val<<" ";
			head1 = head1->next;
			head2 = head2->next;
		}
	}
}

4. The complete code

#include<iostream>
using namespace std;

typedef struct ListNode
{
	int val;
	ListNode *next;
}*List;

void merge(List head1,List head2)
{
	while(head1 != NULL && head2 != NULL)
	{
		if(head1->val < head2->val)
		{
			head1 = head1->next;
		}
		else if(head1->val > head2->val)
		{
			head2 = head2->next;
		}
		else
		{
			cout<<head1->val<<" ";
			head1 = head1->next;
			head2 = head2->next;
		}
	}
}

int main()
{
	List head1 = (List)malloc(sizeof(ListNode));
	head1->next = NULL;
	List head2 = (List)malloc(sizeof(ListNode));
	head2->next = NULL;
	
	List r1,r2,p1,p2;
	r1 = head1;
	r2 = head2;

	//初始化head1(1,2,3,4)
	for(int i = 0;i < 4;i++)
	{
		p1 = (List)malloc(sizeof(ListNode));
		p1->val = i + 1;
		r1->next = p1;
		r1 = p1;
	}
	r1->next = NULL;

	//初始化head2(0,2,4,6)
	for(int i = 0;i < 4;i++)
	{
		p2 = (List)malloc(sizeof(ListNode));
		p2->val = i * 2;
		r2->next = p2;
		r2 = p2;
	}
	r2->next = NULL;

	merge(head1->next,head2->next);
	system("pause");
	return 0;

}

5. The output

1,2,3,4 chain 1, chain 2 is 0,2,4,6, duplicate portions 2 and 4 outputs.
Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1381

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103711862