5
1)  Given only a pointer to a node to be deleted in a singly linked list, how do you delete it? 2)  How would you find out if one of the pointers in a linked list is corrupted or not? 3)  Remove a loop in a linked list with the constant memory constraints? 4) How do you sort a linked list? Write a C program to sort a linked list. 5) Given a linked-list and 2 integers k & m. Reverse the linked-list till k elements and then traverse till m elements and repeat. 6) Select a random node data from a very long linked list whose length is not known such that the probability of each node is equal. 7) Write a function that will return true if a circular singly linked list has duplicate values. For example, given a pointer to a node in the circular singly linked list, *slist, where the only values each node of this list contains int value, and *nxt_pointer. How would you traverse it and what way will allow you to have the best case for time-complexity? How would we know when the circular singly linked list stops? 8) Given two singly linked list, find if they are intersecting. Do this in single iteration. Also find the intersecting node in O(n) time and O(1) space. By intersection I mean intersection by reference not by value 9) how to sort a single linked list with out using an additional node? 10) RotateK times in  a Linked List

Linked List for beginners

Embed Size (px)

DESCRIPTION

learn about data structure

Citation preview

Page 1: Linked List for beginners

1)  Given only a pointer to a node to be deleted in a singly linked list, how do you 

delete it? 

2)  How would you find out if one of the pointers in a linked list is corrupted or 

not? 

3)  Remove a loop in a linked list with the constant memory constraints?

4) How do you sort a linked list? Write a C program to sort a linked list.

5) Given a linked­list and 2 integers k & m. Reverse the linked­list till k elements 

and then traverse till m elements and repeat.

6) Select a random node data from a very long linked list whose length is not 

known such that the probability of each node is equal.

7) Write a function that will return true if a circular singly linked list has 

duplicate values. For example, given a pointer to a node in the circular singly 

linked list, *slist, where the only values each node of this list contains int value, 

and *nxt_pointer. How would you traverse it and what way will allow you to 

have the best case for time­complexity? How would we know when the circular 

singly linked list stops?

8) Given two singly linked list, find if they are intersecting. Do this in single 

iteration. Also find the intersecting node in O(n) time and O(1) space. By 

intersection I mean intersection by reference not by value

9) how to sort a single linked list with out using an additional node?

10) RotateK times in  a Linked List

Page 2: Linked List for beginners

Given: k=3

LinkedList: 10­>20­>30­>40­>50­>60­>70­>80

Output:

30­>40­>10­>20­>60­>50­>70­>80

11) Given a singly link list and a number 'K', swap the Kth node from the start with 

the Kth node from the last. Check all the edge cases.

Sample Input: 1­>2­>3­>4­>5­>6­>7­>8 and K = 3

Sample Output : 1­>2­>6­>4­>5­>3­>7­>8

Sample Input: 1­>2­>3­>4­>5­>6­>7­>8 and K = 10

Sample Output: print error "LIST IS OF LESSER SIZE".

12)

13)  Linked list is : 1­>9­>3­>8­>5­>7­>7

do you see any pattern in this input ?

odd placed nodes are in increasing order and even placed nodes are in 

decreasing order.

write a code that gives the the following linkedlist: 

output linked list should be 1­>3­>5­>7­>7­>8­>9

?? can it be done inplace ?

14) Given a linked list, findout wether it is a palindrome or not,    I have given a 

solution with extra space,

No extra space is allowed,

 

15) Write a code to reverse alternate nodes in a link list.

Page 3: Linked List for beginners

 

16) Given two linked lists, find if they merge and where they merge.

17)Linked List with following structure..

struct node

{

    int data;

    struct node *next;

    struct node *next_larger;

}

initially next_larger of every node is point to NULL.

now write a c code which set all node's next_larger pointer.

where next_largest point to the next larger then its own value and largest value 

node's next_larger pointer points to NULL

i.e.

if LL is 3­>1­>5­>6­>4

then 3's next_larger points to 4 

and 1's next_larger points to 3

and 5's next_larger points to 6

and 6's next_larger points to NULL

and 4's next_larger points to 5

 

18) You have a link list with the following structure:

struct Node{ Node*next; Node*other; }

next pointer points to next node, but "other" pointer points to any node in the 

list, it can be itself or null. you receive the header of a list with this 

structure.   you have to copy it(allocate new memory) , you cannot modify the    

Page 4: Linked List for beginners

structure, you can not modify the list you are given.

19) You have given a linked list in which each node have three items, 1) data, 2) a 

next pointer and 3) a random pointer which is pointing to its successor in sorted 

order. Replicate it ? (Need to generate a new linked list in O(n) + O(n) compl

 

20) Given a linked list and two integers M and N. Scan the linked list such that you 

retain M nodes then delete next N nodes and continue the same till the end of 

linked list.

21) Input linked list:

1­>2­>3­>4­>a­>b­>c­>d­>5­>6­>e­>f

Output should be in below format :

1­>a­>2­>b­>3­>c­>4­>d­>5­>e­>6­>f

 

22) Find the maximum subsequence sum in a linked list. Consider the node as 

shown below.This node class has a extra item isvertex which determines 

whether the node is a vertex r not.

so find the longest distance between any 2 vertex in the linked list.

Node SLL{

int data;

Node n;

bool isVertex;

}

23) If the head of a linked list is pointing to kth element, then how will you get the 

Page 5: Linked List for beginners

elements before kth in C

24) Given an array of pointers to some nodes in a doubly linked list, find the 

number of connected node blocks the pointers point to.

 

25) You have a linked list in which each node is another linked list. How do you find 

the 7th most unique element in the among all the nodes in the most optimal 

time ?

26) Find the maximum subsequence sum in a linked list. Consider the node as 

shown below.This node class has a extra item isvertex which determines 

whether the node is a vertex r not.

so find the longest distance between any 2 vertex in the linked list.

Node SLL{

int data;

Node n;

bool isVertex;

}