算法学习08:打印两个有序链表的公共部分c++实现

本文介绍了一种方法,通过比较两个有序链表的头节点开始,逐步移动较小节点的指针,直到找到公共部分并打印。代码实现了在O(N)的时间复杂度和O(1)的额外空间复杂度下完成这一任务。此外,还提供了使用STL中list的解决方案,同样遵循了时间复杂度和空间复杂度的要求。
摘要由CSDN通过智能技术生成

题目

给定两个有序链表的头指针head1和head2,打印两个链表的公共部分

要求

如果两个链表的长度之和为N,时间复杂度要求为O(N),额外空间复杂度要求为O(1)

分析

首先让两个序列的头指针止住对应有序链表的头部,然后比较这两个指针所指元素的大小,谁的值小,对应序列的指针就后移,然后继续比较,谁小谁后移。若两者元素大小相等,则打印输出,之后同时后移。直到有一个序列的指针越界后,程序停止。

代码

首先需要创建一个链表结点结构

struct node {
    int value;
    node *next;
};

添加结点功能,首先创建新节点,值为value,指针域为null。再new一个节点p_node用于遍历查找尾结点位置,他的初始值为头结点。若链表为空,即头结点是null,直接将new_head作为头结点,若头结点不为null,不断循环查找链表的下一个元素,直到到达链表尾部,将new_code添加到尾部。最终将该链表的头结点返回即可。

node *add_node(node *head, int value) {
    node *new_node = new node();
    new_node -> value = value;
    new_node -> next = NULL;
    node *p_node = new node();
    p_node = head;
    if(head == NULL)
        head = new_node;
    else {
        while(p_node -> next != NULL) {
            p_node = p_node -> next;
        }
        p_node -> next = new_node;
    }
    return head;
}

打印公共部分查找条件head1!=NUll && head2!=NULL,谁小谁移动,相等打印都后移。若一个序列越界,程序结束。

void print_common_part(node *head1, node *head2) {
    while(head1  != NULL && head2  != NULL) {
        if(head1 -> value < head2 -> value)
            head1 = head1 -> next;
        else if(head1 -> value > head2 -> value)
            head2 = head2 -> next;
        else {
           cout << head1 -> value << ' ';
           head1 = head1 -> next;
           head2 = head2 -> next;
        }
    }
}

完整代码

#include <iostream>
#include <string>
using namespace std;

struct node {
    int value;
    node* next;
};

node* add_node(node* head, int value) {
    node* new_node = new node();
    new_node->value = value;
    new_node->next = NULL;
    node* p_node = new node();
    p_node = head;
    if (head == NULL)
        head = new_node;
    else {
        while (p_node->next != NULL) {
            p_node = p_node->next;
        }
        p_node->next = new_node;
    }
    return head;
}

void print_common_part(node* head1, node* head2) {
    while (head1 != NULL && head2 != NULL) {
        if (head1->value < head2->value)
            head1 = head1->next;
        else if (head1->value > head2->value)
            head2 = head2->next;
        else {
            cout << head1->value << ' ';
            head1 = head1->next;
            head2 = head2->next;
        }
    }
}

int main() {
    node* head1 = NULL;
    node* head2 = NULL;
    string str, str_s;
    cout << "please input the data of list1!" << endl;
    getline(cin, str);
    while (str.find(' ', 0) != string::npos) {
        head1 = add_node(head1, atoi(str.substr(0, str.find(' ', 0)).c_str()));
        str.erase(0, str.find(' ', 0) + 1);
    }
    head1 = add_node(head1, atoi(str.c_str()));
    cout << "please input the data of list2!" << endl;
    getline(cin, str);
    while (str.find(' ', 0) != string::npos) {
        head2 = add_node(head2, atoi(str.substr(0, str.find(' ', 0)).c_str()));
        str.erase(0, str.find(' ', 0) + 1);
    }
    head2 = add_node(head2, atoi(str.c_str()));
    cout << "the common part is: " << endl;
    print_common_part(head1, head2);
    system("pause");
    return 0;
}

使用stl中list,实现这个功能

#include <iostream>
#include <list>
#include <iterator>
using namespace std;
 
int main()
{
	int m,n,temp;
	//输入两个链表的长度m,n
	cin>>m>>n;
	list<int> a;
	list<int> b;
 
	//向链表a和b中放元素
	for (int i=0;i<m;i++)
	{
		cin>>temp;
		a.push_back( temp );
	}
 
	for (int i=0;i<n;i++)
	{
		cin>>temp;
		b.push_back(temp);
	}
	//使链表有序
	a.sort();
	b.sort();
	//类似外排的方法来找出公共部分
	list<int>::iterator p = a.begin();
	list<int>::iterator q = b.begin();
 
	while( p!=a.end() && q!=b.end())
	{
		if(*p < *q)
			p++;
		else if(*p > *q)
			q++;
		else
		{
			cout<<*p<<" "<<endl;
			p++;
			q++;
		}
	}
 
	system("pause");
	return 0;
 }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值