📜  门| GATE-CS-2006 |第 80 题(1)

📅  最后修改于: 2023-12-03 15:28:42.813000             🧑  作者: Mango

GATE-CS-2006 Question 80

This is a question from the GATE-CS-2006 exam. It tests the candidate's understanding of data structures and algorithms.

Problem Statement

The problem states that we have two linked lists, each of which represents a polynomial. The task is to add the two polynomials and return the resultant polynomial.

The polynomials are represented as singly linked lists. Each node of the linked list represents a term of the polynomial, and has two fields:

  • coeff: which represents the coefficient of the term
  • power: which represents the power of the term (exponent)

The nodes of the linked list are arranged in decreasing order of power. That is, the first node represents the highest power term, the second node represents the second-highest power term, and so on. The end of the linked list is marked by a NULL pointer.

The problem statement provides an example of two polynomials:

3x^5 - 8x^3 + x^2
10x^6 + 2x^3 - 3x

These can be represented as linked lists as follows:

3 -> 5 -> -8 -> 3 -> 1 -> NULL
10 -> 6 -> 2 -> 3 -> -3 -> 0 -> NULL

Here, the first polynomial has terms with powers 5, 3, and 2, with corresponding coefficients 3, -8, and 1. The second polynomial has terms with powers 6, 3, and 1, with coefficients 10, 2, and -3.

The task is to add these two polynomials and return the resultant polynomial in the same linked list format.

Solution Approach

The solution approach involves traversing both the linked lists simultaneously, starting from the head nodes. We add the coefficients of the terms with the same power, and create a new node for the resultant polynomial with the sum of the coefficients and the same power. If a term is present in only one of the polynomials, then we simply add it to the resultant polynomial.

At the end of the traversal, we return the head node of the resultant polynomial.

Complexity Analysis

The time complexity of the solution approach is O(m + n), where m and n are the lengths of the two linked lists, respectively. This is because we traverse each linked list once, and perform constant time operations for each term.

The space complexity of the solution approach is O(m + n), because we create a new node for each term in the resultant polynomial.

Code Implementation

The code implementation for the solution approach is as follows:

typedef struct node {
    int coeff;
    int power;
    struct node *next;
} Node;

Node *add_polynomials(Node *p1, Node *p2) {
    Node *result = NULL;
    Node **ptr = &result;
    while (p1 != NULL && p2 != NULL) {
        if (p1->power > p2->power) {
            *ptr = p1;
            p1 = p1->next;
        } else if (p1->power < p2->power) {
            *ptr = p2;
            p2 = p2->next;
        } else {
            Node *new_node = malloc(sizeof(Node));
            new_node->coeff = p1->coeff + p2->coeff;
            new_node->power = p1->power;
            *ptr = new_node;
            p1 = p1->next;
            p2 = p2->next;
        }
        ptr = &((*ptr)->next);
    }
    if (p1 != NULL) {
        *ptr = p1;
    } else if (p2 != NULL) {
        *ptr = p2;
    }
    return result;
}

Note that we use a double pointer to keep track of the current node in the resultant polynomial, because we need to modify the next pointer of the previous node to point to the current node.

Conclusion

In this question, we saw a problem that asked us to add two polynomials represented as linked lists. We discussed a solution approach that involved traversing the linked lists and adding the coefficients of terms with the same power. We also analyzed the time and space complexity of the solution approach. Finally, we provided an implementation of the solution approach in C.