0% found this document useful (0 votes)
2 views8 pages

Linked List

The document contains assignment questions and solutions related to LinkedLists, including cloning a double LinkedList with random pointers and splitting a Circular LinkedList into two halves. It also includes multiple-choice questions about LinkedLists and their properties. Solutions are provided with code examples for both tasks.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Linked List

The document contains assignment questions and solutions related to LinkedLists, including cloning a double LinkedList with random pointers and splitting a Circular LinkedList into two halves. It also includes multiple-choice questions about LinkedLists and their properties. Solutions are provided with code examples for both tasks.

Uploaded by

jj491bendreacc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LinkedList

Assignment Questions

Assignment Questions

LinkedList

Assignment Questions

1) Given a double LinkedList. There are 2 types of pointers in this. One pointer is the same as a single LinkedList
which will point to the next node while another pointer is pointing to any random node in the linkedlist. You
need to clone this linkedlist.

Input -> [10,12,32,14,55] - with random pointers

Output -> [10,12,32,14,55] - with same random pointers

2) Given a Circular LinkedList. You need to split it into 2 halves.

Input -> 22,6,12,31

Output -> First Half -> 22,6

Second Half -> 12 31

MCQ Questions
1) Which node will be the top element of the stack when we implement the stack using LinkedList?

A) First node

B) Last node

C) Second node

D) None

2) What is a circular LinkedList?

A) Elements are linked together in a sequential manner

B) There is no start or endpoint

C) Elements are arranged hierarchically

D) None

3) LinkedList is a data structure where elements are fetched using pointers

A) True

B) false

4) Which operation can be performed efficiently using doubly link list?

A) Deleting

B) Traversing

C) Inserting

D) None

5) Which operations can be performed using O(1) complexity in single LinkedList?

A) Inserting the last node

B) Inserting the first node

C) Deleting the last node

D) None
6

Assignment Questions

5) When we delete an element from the index which is not present in the array, which condition is that?

A) Overflow

B) Underflow

C) Garbage

D) None

6
LinkedList

Assignment Solutions

Assignment Solutions

LinkedList

Assignment Solutions

Solution -> https://jsfiddle.net/drgty0Le/

class Node {

constructor(x) {

this.data = x;

this.next = this.random = null;

function print(start) {

var ptr = start;

while (ptr != null) {

document.write(

"Node = " +

ptr.data + ", Random Pointer = "

+ ptr.random.data+"<br/>"

);

ptr = ptr.next;

function clone(start) {

var curr = start, temp = null;



while (curr != null) {

temp = curr.next;

curr.next = new Node(curr.data);

curr.next.next = temp;

curr = temp;

curr = start;

while (curr != null) {

if (curr.next != null)

curr.next.random = (curr.random != null) ?

curr.random.next : curr.random;

curr = (curr.next != null) ?

curr.next.next : curr.next;

}
6
Assignment Solutions

var original = start, copy = start.next;



temp = copy;

while (original != null && copy != null) {

original.next = (original.next != null) ?

original.next.next : original.next;

copy.next = (copy.next != null) ?

copy.next.next : copy.next;

original = original.next;

copy = copy.next;

return temp;

var start = new Node(10);

start.next = new Node(12);

start.next.next = new Node(32);

start.next.next.next = new Node(14);

start.next.next.next.next = new Node(55);



start.random = start.next.next;

start.next.random = start;

start.next.next.random =

start.next.next.next.next;

start.next.next.next.random =

start.next.next.next.next;

start.next.next.next.next.random =

start.next;

document.write("Input LinkedList: <br/>");

print(start);

document.write("Clone LinkedList : <br/>");

var cloned_list = clone(start);

print(cloned_list);
6
Assignment Solutions

2. Solution -> https://jsfiddle.net/emqrbv74/

class Node

constructor(d)

this.data = d;

this.next = this.prev = null;

let head, head1, head2;


function splitList()

let slow_ptr = head;

let fast_ptr = head;


if (head == null) {

return;

while (fast_ptr.next != head

&& fast_ptr.next.next != head) {

fast_ptr = fast_ptr.next.next;

slow_ptr = slow_ptr.next;

if (fast_ptr.next.next == head) {

fast_ptr = fast_ptr.next;

head1 = head;

if (head.next != head) {

head2 = slow_ptr.next;

fast_ptr.next = slow_ptr.next;

slow_ptr.next = head;


6
}
Assignment Solutions

function printList(node) {

let temp = node;

if (node != null) {

do {

document.write(temp.data + " ");

temp = temp.next;

} while (temp != node);

head = new Node(22);

head.next = new Node(6);

head.next.next = new Node(12);

head.next.next.next = new Node(31);

head.next.next.next.next = head;

document.write("Circular Linked list <br>");

printList(head);

splitList();

document.write("<br>");

document.write("First Halves <br>");

printList(head1);

document.write("<br>");

document.write("Second Halves <br>");

printList(head2);

MCQ Answers

A
B
A
A
B

You might also like