The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+7 votes
628 views
Consider a singly linked list having $n$ nodes. The data items $d_1, d_2, \dots d_n$ are stored in these $n$ nodes. Let $X$ be a pointer to the $j^{th}$ node $(1 \leq j \leq n)$ in which $d_j$ is stored. A new data item $d$ stored in node with address $Y$ is to be inserted. Give an algorithm to insert $d$ into the list to obtain a list having items $d_1, d_2, \dots, d_{j}, d,\dots, d_n$ in order without using the header.
asked in DS by Veteran (59.6k points)
recategorized by | 628 views

2 Answers

+17 votes
Best answer

Following $2$ lines are enough to realise above constraint :->>

1. Y->next = X-> next

2. X->next = Y

answered by Boss (23k points)
edited by
+3

d->next = dj->next

dj->next = d

+7 votes

these steps are mandatory for the algorithm :
$\begin{align*} temp &= X \rightarrow next\\ X \rightarrow next &= Y \\ Y \rightarrow next &= temp \end{align*}$

answered by Boss (30.8k points)

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,658 questions
48,641 answers
156,234 comments
63,954 users