Skip to content
GATEiit

GATE CSE 2017 Session 1 Q18: C linked list join(m, n): what does it do

Syllabus topics: Arrays, stacks, queues and linked lists; Programming in C

Question

Consider the C code fragment given below.

typedef struct node {
   int data;
   node* next;
} node;

void join(node* m, node* n){
   node* p = n;
   while(p->next != NULL) {
      p = p->next;
   }
   p->next = m;
}

Assuming that m and n point to valid NULL-terminated linked lists, invocation of join will

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation

Answer: B (official GATE 2017 final answer key)

Concept used: Walking a linked list via p->next dereferences p, so an empty list (NULL head) must be handled separately.

Concept notes

Loading notes…

First step: Consider what happens on the first loop test when n is NULL (an empty but valid list), and what happens otherwise.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Arrays, stacks, queues and linked lists

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2017 Session 1 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key