Showing posts with label Linked List. Show all posts
Showing posts with label Linked List. Show all posts

Saturday, April 14, 2012

Union and Intersection of two Linked Lists in C++



Output :-
List1 : 78  19  12  30  10
List2 : 89  19  121  30
Union List : 89  121  10  30  12  19  78
Intercestion List : 30  19

Friday, September 16, 2011

Nth last element in Linked List (C++)

The following code shows how to get nth last element in a linked list. It uses a queue to hold the temporary elements.

Algorithm:-
1>Make a queue of length N
2>Traverse the linked list from root
3>While traversing, en-queue first N elements
4>After this for en-queuing every element, de-queue another element.
5>When we reach the end of linked list, the last de-queued element will be the Nth last element.



Output:-