题目
给定两个有序链表的头指针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;
}