Linked List Merge 2024 (Optional)

Time limit: 500ms
Memory limit: 256mb

Description:
In this exercise, your task is to merge one integer linked list p_2 to another integer linked list p_1. You can assume that the integers in both linked lists are not the same. After the merge p_1 store sorted distinct integers in descending order. 

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;

List Merge(List p_1, List p_2){
    // WRITE YOUR CODE HERE
}

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);
	Node *newNode = (Node *)malloc(sizeof(Node));
	newNode->value = value;
	newNode->next = p1;
	p_1 = newNode;
    }

    // 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);
        Node *newNode = (Node *)malloc(sizeof(Node));
	newNode->value = value;
	newNode->next = p2;
	p_2 = newNode;
    }

    // Merge lists
    List p = Merge(p_1, p_2);

    // Print current list
    while(p!=NULL)
    {
        printf("%d ", p->value);
        p = p->next;
    }
    printf("\n");

    return 0;
}


-------------------------------------------End of Code-------------------------------------------

Submit