Merge Two Linked Lists
Time limit: 2000ms
Memory limit: 256mb
Description:
In this exercise, we use the singly linked list to maintain a sequence of distinct integers in ascending order.
To keep the integers in order after insertion, we can not just insert the new value to the head of the linked list. Instead, we should insert it to an appropriate position. Although we have provided the insert function, a deep understanding of it may help you solve the next problem.
Now, we have two linked lists which represent two sets of sorted integers denoted by A and B. You are required to implement the following functions.
1. Create a new sorted list as the intersection of A and B.
2. Create a new sorted list as the union of A and B.
3. Print all integers of the linked list in order.
List Intersection(List headA, List headB);
Generate a linked list "head" to contain the intersection of A and B in ascending order.
You can only visit the input linked list "headA" and "headB" but not change them. Finally, return "head".
List Union(List headA, List headB);
Generate a linked list "head" to contain the union of A and B in ascending order.
You can only visit the input linked list "headA" and "headB" but not change them. Finally, return "head".
void Print(List p);
Print all integers of linked list "p" from head to tail in one line.
Every two integers are separated by a space. Print a '\n' in the end.
If the list contains no integers, output a blank line.
Sample Input 1:
4
4 10 1 7
5
7 3 4 11 2
Sample Output 1:
Input the number of integers in A:
Input these integers:
Input the number of integers in B:
Input these integers:
1 4 7 10
2 3 4 7 11
The intersection list contains:
4 7
The union list contains:
1 2 3 4 7 10 11
Sample Input 2:
2
2 3
3
6 5 1
Sample Output 2:
Input the number of integers in A:
Input these integers:
Input the number of integers in B:
Input these integers:
2 3
1 5 6
The intersection list contains:
The union list contains:
1 2 3 5 6
Hint:
Be careful of the efficiency issue. You'd better not use the insert function provided to generate the intersection/union linked list. The intersection/union fucntion should be implemented in linear time.
Code Template:
#include <stdio.h>
#include <stdlib.h>
typedef struct linked_list_node{
int value;
struct linked_list_node* next;
} Node;
typedef Node* List;
List Insert(List head, int value)
{
Node *ptr = (List)malloc(sizeof(Node)); // allocate a piece of memory for the new node
ptr->value = value;
if(head==NULL || value<head->value)
{
ptr->next = head;
return ptr;
}
Node *current = head;
while(1)
{
if(current->next==NULL || value<current->next->value)
{
ptr->next = current->next;
current->next = ptr;
break;
}
else current = current->next;
}
return head;
}
List Intersection(List headA, List headB)
{
//Generate a linked list head to contain the intersection of A and B in ascending order.
//You can only visit the input linked list headA and headB but not change them. Finally, return head.
List head = NULL;
return head;
}
List Union(List headA, List headB)
{
//Generate a linked list head to contain the union of A and B in ascending order.
//You can only visit the input linked list headA and headB but not change them. Finally, return head.
List head = NULL;
return head;
}
void Print(List p)
{
//do not forget to print a '\n'
}
int main()
{
List headA = NULL;
List headB = NULL;
int n, m;
int value;
printf("Input the number of integers in A:\n");
scanf("%d", &n);
printf("Input these integers:\n");
for(int i=0; i<n; i++)
{
scanf("%d", &value);
headA = Insert(headA, value);
}
printf("Input the number of integers in B:\n");
scanf("%d", &m);
printf("Input these integers:\n");
for(int i=0; i<m; i++)
{
scanf("%d", &value);
headB = Insert(headB, value);
}
List headC = NULL;
List headD = NULL;
headC = Intersection(headA, headB);
headD = Union(headA, headB);
Print(headA);
Print(headB);
printf("The intersection list contains:\n");
Print(headC);
printf("The union list contains:\n");
Print(headD);
return 0;
}
Submit