But this is not a generic solution and will not be fit for PROJECT's.
Let's say i have pass unknowingly a circular list to your written function. Will it works in that case? also if the list is having a loop then how will you handle it?

@subrata
If a list is circular or it contain a loop, then what is the last node of this link list?
There would be none.
And so the question becomes invalid.
Therefore the question is only for singly linked list that do not contain any loops, and therefore the answer is good.

@sushmita: it works for all value of k and n..
@Subhransu: no it is nt possible to obtain order less than n, as we hv to scan the list atleast once in order to find kth element from the end. If we know the no. of nodes n in linked list in advance, only then it is possbl to obtain order less than n

I think it will work but there is a small issue ...you are swapping the data(or its pointer) instead of changing a pointer itself...And in almost all the cases the answer is not acceptable
For example
Assume that the node consists of object or data pointers to more than one data... in that case you are supposed to rearrange the nodes using the pointers rather than swapping the data(or its ptrs) [this is what my interviewer said to me at interview at MS and which seems fair enough]

So similar thing can be achieved by just rearranging next pointers leaving data untouched (obviously i will have to save the pointers to the elements prev to the elements to be swapped)

@sushmita Sorry to see people being treated like this. Try to follow the code for actual examples to convince yourself if it works for the general case.@AbbySol I don't know what kind of bug you have in your social programming, but I don't know how you construed her asking for clarification as being overly critical.

As for the code: if you know the second pointer is going to have "head" as its initial value (once you start using it), why don't you just assign that value to it after its declaration? Also, I don't see a need for the if-conditional checking the pointers for NULL, because you have already done so in the while loop. I am assuming that those pointers are not meant to refer to memory concurrently accessed as we swap or this implementation would have other issues to worry about.

It seems like the following would be a functionally equivalent implementation:

However, I have the same concern with this question as @vik above: if the linked list owns the contents of the nodes, copying contents may be permissible. With a general purpose container, that is usually not acceptable. The caller is most likely owning the objects they inserted and has references to them. If we swap out data like this, the external references will suddenly refer to different objects.

Imagine, for example that each item in the linked list is a "car" object. Each car has an owner which holds a pointer to the car they own. The linked list is the order in which the cars are serviced at a car dealership. If you swap and copy car objects like that you end up with the cars in the new positions, but owners suddenly end up owning different cars than before.

There are other issues with this, such as assuming that it's okay to copy objects like that. Many examples preclude this. It is one of the reasons why it's a bad idea to put std::auto_ptr<T> instances into an STL container. Copying such an instance transfers ownership of the pointer being wrapped and an std::sort(...) could create copies and lose ownership that way.

I will follow up with another post in a second for a solution swapping the pointers.

We need to handle a few test cases.
1. Both nodes are end nodes.
2. Both nodes are adjacent.
3. Both nodes are somewhere else.
4. Both node are same.
5. kth node doesn't exist.

I prefer swapping the nodes rather than values. It is always advisable to swap the nodes as in general, nodes may contain several data. So, it will be overhead to swap the values.

Steps:
Find the kth node[p] from the beginning. If no kth node, return.
Take another pointer[q] & move it at head. Move it & the kth node one step at a time until kth node is null. q is the kth node from the last.

We need to swap p & q.
The part of linked list found is: t1->p->t2 and t3->q->t4
Just swap the nodes based on links & handle the corner cases mentioned above.
Here is the code:

Have you tested this code for the case that the first and last node are to be swapped and the list contains two nodes, i.e. nodes are adjacent AND the head needs to be updated? Sorry for not having tried this myself. If I don't hear from you, I will compile your code and try myself tomorrow.

Start moving P1 & P3 till you have moved 'K' nodes. If the list ends before P1 reaches the End, give the error message "LIST IS OF LESSER SIZE".
Once you have reached the Kth node, move P1 and P2 pointers to the next node one by one till P1 reaches the end.

At this point P2 points to the Kth element from the last.
Now simply swap the values of P2 & P3.

Algorithm:
1.Get the linked list lenghth. If it is less than 'K' print the error message. O(N)
2.Traverse to the 'K' the element from the beginning. O(N)
3.Traverse to the N-Kth element from the beginning. O(N)
4. Swap them.O(1)
5.End of Algorithm

1.take 2 pinters say ptr1 and ptr2 and assign the root node address.
2.Move the ptr2 for the k times,this the pointer you have to swap keep the address with you.
3.Now move the both the pointer ptr1 and ptr2 both at time, the moment ptr2 reach to null, note down the ptr1.
4.swap the ptr1 and once node which you have restored before.

Let n1, n2, and n3 be Node variables. Let n1 and n2 = head. Iterate n1 K nodes down the list. If n1.next is null before you have completed the traversal, the list is smaller than K. Once you have iterated K nodes, let n3 = n1. Traverse both n1 and n2 until the n1 is at the end of the list. At this point, n3 is pointing at the Kth node from the head, and n2 is pointing at the Kth node from the tail. Swap the data and you're done.

Algorithm:
1.Get the linked list lenghth. If it is less than 'K' print the error message. O(N)
2.Traverse to the 'K' the element from the beginning. O(N)
3.Traverse to the N-Kth element from the beginning. O(N)
4. Swap them.O(1)
5.End of Algorithm

What's Going On

Books

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Resume Review

Most engineers make critical mistakes on their resumes -- we can
fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.