Linked List Merge 2023 (Optional)
Time limit: 500ms
Memory limit: 256mb
Description:
Based on Exercise 2, your task is to merge one integer linked list p_2 to another integer linked list p_1. Both p_1, p_2 store sorted distinct integers in descending order, before and after the merge.
List Merge(List p_1, List p_2)
Example:
Given two lists: p_1 = 5 -> 4 -> 3 -> 2 -> 1 and p_2 = 10 -> 9 -> 8 -> 7 -> 6, merge p_2 to p_1
List p_1 then becomes: 10 -> 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1
Sample input:
5
2 4 5 3 1
5
6 9 8 7 10
Sample output:
10 9 8 7 6 5 4 3 2 1
-------------------------Copy the following code, complete it and submit-------------------------
#include <stdio.h>
#include <stdlib.h>
typedef struct linked_list_node{
int value;
struct linked_list_node* next;
} Node;
typedef Node* List;
int Exist(List p, int value)
{
// judge whether linked list p contains the value
// you should visit each node of the linked list one by one, and check whether it is equal to the value
// if you find it then return 1, otherwise return 0
while(p!=NULL)
{
if(p->value==value) return 1;
p = p->next;
}
return 0;
}
void Print(List p)
{
// print all integers from the head of linked list in one line(separated by a space), in other words, you should firstly print the integer p points to
// to be more specific, print p->value and then move p to the next node p->next recursively, until p points to NULL
while(p!=NULL)
{
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
List Insert(List p, int value)
{
}
List Merge(List p_1, List p_2){
}
int main()
{
List p_1 = NULL;
List p_2 = NULL;
int n;
int value;
// List initialization
// How many integers in FIRST list initially
scanf("%d", &n);
// What are the integers
for(int i=0; i<n; i++)
{
int value;
scanf("%d", &value);
p_1 = Insert(p_1, value);
}
// List initialization
// How many integers in SECOND list initially
scanf("%d", &n);
// What are the integers
for(int i=0; i<n; i++)
{
int value;
scanf("%d", &value);
p_2 = Insert(p_2, value);
}
// Merge lists
List p = Merge(p_1, p_2);
// Current list
Print(p);
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Submit