This is a common test question. Have you tried to solve it yourself? Here's a hint: you can move data as well as change links.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
You only need to copy one data element and change one link.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
>Ya, I know u can shift up the links following the one pointed to.
Shift? It sounds like you're confused, but we'll proceed.
>Or u can swap with the last node and delete the last node(If
>sorted order is not a criteria).
Sorted order is irrelevant since you're unlinking the node causing disorder anyway. And you don't need to swap unless you're returning the deleted node and need the data in the calling function.
>Both the above methods are not practical in such a scenario
>as both are computational intensive.
Constant time performance with two operations is computationally intensive? The only way to improve that performance is to drop down to inline assembly and optimize the data movement with low level register tricks. Even then you won't see much of an improvement.
You say you understand the solution I was suggesting, but you obviously don't if you think it's "computationally intensive".
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
i like agressive
RrrRRraawwrrrrr.....
Clinton Portis
Practically a Posting Shark
833 posts since Oct 2005
Reputation Points: 237
Solved Threads: 118
Narue is not so much agressive as frustrated with the lack of effort by many (especially younger) people in this world to even attempt to solve their own problems, an attitude I can very much relate to.
But then we're both professionally employed in this profession and have to work with the end result of an educational cycle in which such lack of effort gets people degrees and diplomas which in turn get them hired to be our colleagues, saddling us with the burden of doing their work to get projects completed.
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
Narue is not so much agressive as frustrated with the lack of effort by many (especially younger) people in this world to even attempt to solve their own problems, an attitude I can very much relate to.
But then we're both professionally employed in this profession and have to work with the end result of an educational cycle in which such lack of effort gets people degrees and diplomas which in turn get them hired to be our colleagues, saddling us with the burden of doing their work to get projects completed.
In my school, we have an IT technician, we call himMr. Grumpy because everytime we ask him to fix this or do that he is always grumpy, probably due to the reasons outlined above.
Whilst it is entirely possible that these people can lead socially rewarding lives outside of work I find it difficult to imagine. A happy IT associated person, - are you kidding me? The constraints of life make this unlikely. :lol:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>The better method as per me is
You do realize that the same solution was suggested in this thread three years ago, yes? :icon_rolleyes:
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>If you simply delete, then the previous node link will not have NULL. bad idea.
This statement suggests that you don't understand how nodes are deleted in a singly linked list. To unlink a node, you link the previous node with the next node before releasing memory:
Original:
A->B->~
Unlink B:
+-->-+
| |
A-+ B->+->~
Delete B:
+-->-+
| |
A-+ +->~
Done:
A->~
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401