retagged by
571 views
4 votes
4 votes

Consider the null-terminated linked list of four integers $\textsf{1->2->3->4->NULL},$ and the variable 'list' points to the head of the linked list. Upon running the provided code, the linked list gets divided into two lists, as illustrated in the diagram below.

The C code that was executed is as follows:

struct ListNode {
    int val;
    struct ListNode *next;
}list, list2;
list2 = list->next->next->next; 
list2->next = list; 
//LINE X 
// LINE Y
list->next->next = NULL; 
list2->next->next = NULL; 

To successfully accomplish the task, what should be inserted at $\textsf{LINE X}$ and $\textsf{LINE Y}?$

  1.  LINE X: list->next->next->next = list->next;
     LINE Y: list = list->next->next;
  2.  LINE X: list->next->next = list->next;
     LINE Y: list = list->next->next; 
  3.  LINE X: list = list->next->next;
     LINE Y:  list->next->next->next = list->next; 
  4.  LINE X:list->next->next>next = list->next>next;
     LINE Y: list = list->next->next; 
retagged by

2 Answers

1 votes
1 votes
Given linked list: list → 1 → 2 → 3 → 4→ NULL where list is a pointer variable pointing to first node of linked list or simply it is head pointer for better understanding.

now in code line a list2 pointer variable is given,

list2 = list → next → next → next

       = 1 → next → next → next

      = 2 → next → next

       = 3 → next

       = 4

list2 is pointing to node having data value 4.

list2 → next = list means 4 is pointing to 1.

so up till this point we got one circular linked list.

now we want two node as    list → 3 → 2 and list2 → 4 → 1

now what we have to change the next pointer of 3 pointing to 2  and for that we have to perform

list → next → next → next = list → next ;

3 → next = 2;

now we have to move the list pointer pointing to 3 and for that we have to perform list = list → next → next;

and you got result.

------------------ Variation --------------------------------------------------------

PS:

We can also do it another way

after making circular list, directly take list pointer and point that to 3

list = list → next → next;

then we can make pointing list node next pointer to 2,

list → next = list2 → next → next;

3 → next = 4 → next → next;

3 → next = 1 → next;

and finally we made 3 → next = 2;

so if it is MSQ then this also be correct way.
0 votes
0 votes
Original linked list : list  > 1 > 2  > 3 > 4 | NULL

In Option A :

list > next  > next > next = list > next ;

This makes the list as

list > 1  > 2  > 3  > 2

list = list > next > next;

This further updates as :

list  > 3  > 2

later this is made NULL terminating linked list by the code.

Hence Option A satisfies the linked list given in the question.

open for any correction :)
Answer:

Related questions

490
views
1 answers
3 votes
GO Classes asked Jan 28
490 views
Let $\mathrm{T}$ be the smallest AVL tree of height $h$. How many nodes does it have, if the smallest AVL tree of height $h-2$ has $m$ nodes and the smallest AVL tree of height $h-3$ has $k$ nodes?$m+k+2$m+2 k$2 m+k$2 m+k+2$
972
views
3 answers
7 votes
GO Classes asked Jan 28
972 views
You are given a complete binary tree (each level must be full except the last) on $n$ vertices. Each vertex $v$ is labeled by an integer value $x_v$. Say that a vertex is a ... $\theta(\sqrt{n})$\theta(\log n)$\theta(n \log n)$
570
views
1 answers
6 votes
GO Classes asked Jan 28
570 views
Suppose we constructed the binary search tree shown by starting with an empty tree and inserting one element at a time from an input sequence, without any rotations or other ... $3$ came before $14$ and $16$ came before $28.$
628
views
1 answers
3 votes
GO Classes asked Jan 28
628 views
Suppose that a binary min-heap stores six elements with priorities $10,20,30,40,50$, and $60$ in its array $\text{A}.$ What is the largest of these items that could be stored in $\text{A}[2]?$ (indexing starts from zero)