varalu
Oct 26 2007, 10:01 AM
Given a linked list, find the 5th last element with Time complexity O(n) and minimal space complexity. Note: If you know the answer and if you feel it is simple also please post the answers so that others will come to know about the answers. What is a linked list?? /* this is for your further reference and reading */QUOTE In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of nodes, each containing arbitrary data fields and one or two references ("links") pointing to the next and/or previous nodes. The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk, allowing the list of items to be traversed in a different order. A linked list is a self-referential datatype because it contains a pointer or link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point in the list in constant time, but do not allow random access. Several different types of linked list exist: singly-linked lists, doubly-linked lists, and circularly-linked lists. There are various kinds of linked lists available and can be implemented in many languages and in different ways. Applications of Linked ListsQUOTE Linked lists are used as a building block for many other data structures, such as stacks, queues and their variations.
The "data" field of a node can be another linked list. By this device, one can construct many linked data structures with lists; this practice originated in the Lisp programming language, where linked lists are a primary (though by no means the only) data structure, and is now a common feature of the functional programming style.
Sometimes, linked lists are used to implement associative arrays, and are in this context called association lists. There is very little good to be said about this use of linked lists; they are easily outperformed by other data structures such as self-balancing binary search trees even on small data sets (see the discussion in associative array). However, sometimes a linked list is dynamically created out of a subset of nodes in such a tree, and used to more efficiently traverse that set. even bad solutions are welcome...  .
Reply
pointybirds
Oct 26 2007, 12:45 PM
Hmm, presuming a singly linked list where you can't start at the end and work your way back, you could use a circular buffer with 5 elements (e.g. an array that can store the contents of the LL nodes) and traverse the list until you reach the end ... then, the "next" element in your circular buffer is the fifth last. E.g.: for (i=0; (e = next element) != null; i++%5) { buf[i] = e } fifth_last = buf[i] Something like that I guess. You'd also need to initialise your array to null and check for this at the end in case your LL < 5 elements.
Reply
varalu
Oct 26 2007, 01:34 PM
yours is a nice solution... Better in usage of memory also. This circular buffer thing is very good. What is i am giving you a constraint of 1. "NO EXTRA MEMORY" 2. No additional Data structures. Give a solution with the above constraints. Small Hint:Do not make it complex...think very simple. --> for a simple solution.
Reply
sanjay0828
Oct 26 2007, 07:00 PM
Ok.. here is a very simple solution Have two pointers. Initially, let both the pointers point to the first node of the linked list Now move one of the pointers to the 5th node of the linked list. This can be done in O(1) complexity i.e. constant time. Example:- 1-->2-->3-->4-->5-->6-->7-->8-->9-->10->11-->12 Here, one pointer points to 5 while the other pointer points to 1 Now keep moving each pointer i.e. current node to next node, until the first pointer reaches the end of the linked list i.e. 12 The second pointer will now be pointing to the 5th last element i.e. 8
Reply
jlhaslip
Oct 26 2007, 07:29 PM
$item= $array[count($array)-5]; untested may need to perform this in steps though $num = count($array); $n = $num - 5; $item = $array[ $n ];
Reply
varalu
Oct 27 2007, 09:09 AM
@sanjay0828 Good one... the expected solution. Again... i want a simpler solution. What will be the basic answer that comes to your mind if i give this problem.?? Very basic..  .. If no answer i will be posting it in my next post.
Reply
iGuest
Nov 4 2007, 01:06 AM
Use two variable (sum4 and sum5) to store the sum of last 4 nodes and last 5 nodes respectively. At the end of the loop, the 5th node data = sum5 - sum4. -Grant
Reply
totrap
Nov 23 2007, 06:08 AM
while(ptr->next->next->next->next->next!=NULL) ptr=ptr->next; //after loop, ptr points to the fifth last element in the list;
Reply
songs2enjoy
Aug 31 2008, 10:01 AM
reverse the list and find the 5th element from the start, Not simple though just an idea. Varalu, Can u plz post what's the simple idea ? Thanks
Reply
Recent Queries:--
c code linked list count number of elements - 5.36 hr back. (4)
-
searching for nth element - 9.70 hr back. (1)
-
find a element in circular linked list - 14.28 hr back. (1)
-
example of nth element from last in linked list - 18.43 hr back. (1)
-
find the nth node from the end of singlely linked list - 31.48 hr back. (1)
-
algorithm nth last node - 31.63 hr back. (1)
-
nth element from last in link list - 33.99 hr back. (1)
-
nth element from last in linked list - 35.32 hr back. (1)
-
how would you find the nth to last element in a linked list? - 20.27 hr back. (2)
-
find last element of a linked list - 38.13 hr back. (1)
-
data structure linked list - 39.13 hr back. (1)
-
how to add a element in a linked list - 43.46 hr back. (1)
-
count of element in circular link list - 43.77 hr back. (1)
-
how would you find the nth to last element in a linked list - 45.25 hr back. (2)
Similar Topics
Keywords : data, structures, linked, list, nth, element, linked, list,
- Data Structure -- Permutations
(1)
Data Structure -- Queue -- Implement Using Stack
(1) Implement a Queue using a stack. No restriction on space complexity. One possible Solutions a
costly procedure... 1. Use a temp stack 2. Insertion into queue - Push the element into the
original stack 3. Deletion from queue - Pop all the elements from stack into a temp stack
- pop out the first element from the temp stack - pop all the remaining elements back to the
original stack What is a queue? QUOTE A queue is a particular kind of collection in which
the entities in the collection are kept in order and the principal (or only) operation....
Data Structure -- Trees -- Threaded Binary Tree
(0) A binary tree having a loop is known as a threaded binary tree. Have a look at the attachment to
have an idea of a threaded binary tree.... A's right child is B and B's left child is A.
Write an algorithm to find out whether the given tree is a threaded binary tree. Look for time
and space complexity.....
Data Structures -- Expression Trees
(0) Construct an expression tree for the expression (a || /cool.gif" style="vertical-align:middle"
emoid="B)" border="0" alt="cool.gif" /> && (c || d) After constructing the tree convert the tree to
correspond to the associative property of the given expression. Eg: (1 + 2) * ( 3 + 4) = (1 * 3) +
(1 * 4) + (2 * 3) + (2 * 4) Similar to that, from the constructed expression tree, construct a new
expression tree such that inorder traversal of the new tree will be associative value of the given
expression Inorder traversal of the new tree should be (a && c) || (a && d) || (....
Data Structure -- Arrays -- Odd Number Of Elements
(0) Given an array of elements with many numbers occurring even number of times and two numbers
occurring odd number of times. Find out the two numbers that occur odd number of times. example:
Elements in array -- 14433446 The expected result is 1 and 6 One solution 1. Find max of the
array 2. Hash Function : element/max value 3. Repeat to all elements... 4. Find frequency... yo will
get the 2 elements with odd frequency. but this is not the optimal.... Do find more solutions to
this and post it. Look for time and space complexity. Another Solution one more s....
Data Structures -- String -- Arrange Based On Repetition
String data structure (1) Consider a string with any number of elements occurring any number of time. Rearrange the string
in such a way that the alphabet with most occurrence occurs in the followed by the next most
occurring alphabet and so on... It should also be seen that the alphabets should occur frequency
number of times. Example: Input : abcdaeghzabcdbhb Output : bbbbaaaccddhhegz ....
Data Structures -- Linked List -- Point Of Merging
(2) Given two singly linked lists with both of them merging at some point. Find the position at which
they merge. Eg: 1->30->50->65->2->59 88->46->65->2->59
The above two linked lists merge at 65. Find this node where they merge.. Question
Courtesy: Antony(Friend) One possible Answer A simple answer would be to find the length of
both the linked lists and have two pointers, one for each linked list. Move (difference in lengths)
steps in the longer linked lists and then start moving both the pointers by one every t....
Data Structures -- String -- Palindrome
Check if a string is a palindrome... (5) Write an algorithm to check whether a given string is palindrome or not in time complexity O(n)
What is a palindrome?? QUOTE A palindrome is a word, phrase, number or other sequence of units
that has the property of reading the same in either direction (the adjustment of punctuation and
spaces between words is generally permitted). Composing literature in palindromes is an example of
constrained writing. The word "palindrome" was coined from Greek roots palin
(πάλιν; "back") and dromos (δρóμος; "way,
direction") by....
Data Structures -- Linked List -- Reverse
Reverse a linked list (4) Give an algorithm to reverse a linked list with a time complexity of O(n) and minimal space
complexity. What is a linked list? Search trap17.com. i Have already answered this question in
one of my older questions. Solution 1 Here is one simple solution... CODE Void
ReverseList(node* head) { node *temp,*current,*result; temp=null; result=null;
current=head; while(current!=null) { temp=current->next;
current->next=result; result=current; current=temp; }
head=result; ....
Data Structures -- Binary Tree -- Structurally Same
Find if 2 binary tress are structurally same?? (4) Given two binary trees, find out whether a tree is structurally same to the other. Structurally same
means that the two trees look alike or a tree looks alike a subtree of the other tree. Look for
time and space complexity. Some more for readers What is a binary tree? QUOTE A binary
tree is made of nodes, where each node contains a "left" pointer, a "right" pointer, and a data
element. The "root" pointer points to the topmost node in the tree. The left and right pointers
recursively point to smaller "subtrees" on either side. A null pointer represents a binary....
Data Structures -- Binary Tree -- Mirror Image
Binary Tree -- Mirror Image (0) Given a binary tree, write an algorithm to find its mirror image with minimal time and space
complexities. Note: If you know the answer and if you feel it is simple also please post the
answers so that others will come to know about the answers. This question was sent by my friend
through mail. Solutions Suggested Sol 1 CODE void PrintMirror(node *root) {
if(node!=NULL) PrintMirror(root->right);
Printf(root->data); PrintMirror(root->left); } In case of
implementation using....
Data Structure Questions
(4) Question 1 Reply the solutions if you have any so that we can discuss... Given an array of n
elements (containing only positive numbers) and sum, X. Find the first two elements in the array
that sum upto X eg: Array of elements - {2, 3,1000, 200, 51, 88, 29, 49, 65, 40, 98, 12, 3}
Sum - 100. The answer for the above sample is 51, 49. There are other possiblities
also, but the first two numbers summing upto the given sum, 100 should be taken. How will you do
this with minimal space and time complexities? Question 2 Given two nodes of ....
Limiting Returned Data To Last X Fields With Sql
(2) Hello, I'm currently developing a social-networking site in ColdFusion and several aspects of
the site require me to return a limited number of records for display (e.g. displaying the last 3
news items in the left-hand column on the front page, etc.). I currently only have a small SQL
"cheet sheet" and what my ColdFusion reference book tells me, and I cannot find anything anywhere
about limiting the data returned from a SELECT statement to the last X number of records. Any help
is appreciated! Thanks, zeeman48....
A Question About Data Collecting Program
usually found in registers (1) does anyone know which programming language is used to make applications like those used in shops
and malls where the sales assistant will enter name of a product and all its details will come up
and whn someone makes a purchase the details will go to a database , i need to know which language
is used and also which platform is used ,thanks in advance /laugh.gif"
style="vertical-align:middle" emoid=":lol:" border="0" alt="laugh.gif" /> Please read the rules.
The What Is..? forum is for answering a common question or telling everybody about what
interesting fact you ....
Looking for data, structures, linked, list, nth, element, linked, list,
|
*RANDOM STUFF*
*SIMILAR VIDEOS*
Searching Video's for data, structures, linked, list, nth, element, linked, list,
*MORE FROM TRAP17.COM*
|
advertisement
|
|