Você está na página 1de 2

CSI-DTU 2015

LINKED LISTS
Q. Why merge sort preferred for LL while Quick sort preferred for arrays?
Ans. This is due to diff. in allocation of arrays and LL. Unlike arrays, in LL we can insert items in
middle in O(1) extra space and O(1) time. Therefore, merge operation can be implemented
without extra space for LL.
We can do random access in arrays. Eg: Let us say address of A[0} be x, then to access A[i} we
can directly access memory at (x+ i*4) . But unlike arrays, we cannot do random access in LL
and quick sort requires lot of random access. Therefore, overhead increases for quick sort.
Whereas merge sort accesses data sequentially & need of random access is low.
Important Questions:

Merge k sorted linked lists (use priority queues)


Flattening a linked list
Reversal of a linked list
Loop detection
Check whether loop exists
Find Start node of loop
Find length of loop
Remove the loop
Merging Linked lists
Middle element of LL
Nth node from end in a LL
Display LL from end (recursion)
Reverse LL in groups of given size (k-reverse in a LL)
Josephus Circle
Check Linked list is palindrome or not
Cloning a LL (with random pointers)
Segregate odd and even numbers in a LL
Splitting a circular LL
Head pointing to kth element, how to get elements before kth element (XOR linked lists)
Reverse a DLL
Given a LL, sort without extra space (use merge sort)
Reverse a LL using single pointer
Flatten multilevel LL
Rotating a LL

CSI-DTU 2015

Given a sorted DLL & two numbers c & k. Decrease the data of node with data k by c and
insert new node formed at its correct position such that list remains sorted.
Search in sorted LL (better than O(n) use skip list method)
Add 2 numbers represented by LL

XOR Linked Lists:

It is a memory efficient doubly linked list.


It can be created using only one space for address field with every node unlike in an
ordinary DLL.
Uses bitwise XOR operation to save space for one address instead of storing actual
address, every node stores XOR of addresses of prev & next nodes.
We can traverse XOR linked list both in forward and backward directions. While
traversing we need to remember the address of previously accessed node in order
to calculate next nodes address.
Eg: we are at node C, we must have address of B.
XOR of add(B) and npx of C gives us add(D) because npx(C) is add(B) XOR add(D). Its
XOR with add(B) gives :
Add(B) XOR add(D) XOR add(B) = add(D)
Forward Traversal:
We need to find out the address of next node.
next=XOR(prev,curr->npx);
[because XOR of (prev) & (prev) XOR (next) gives (next)]

Reverse a DLL
Just swap next & prev pointers for all nodes in a DLL.

Você também pode gostar