You need two levels of indirection to do it the way Linus suggests, but you can potentially do it in Python perhaps by having a reference to an object which stores a reference to an object or something of this sort (an index to an index?). That said, I don't think it maps so elegantly or efficiently to Python and it'd probably be quite wasteful to use an object just to represent a single link in a linked structure.
In Python's case I'd just do the additional branching to check for cases where you're removing from the head unless there's some trick I'm missing.
As for implementing linked lists yourself, I actually find many use cases where the standard libraries don't suffice. Here's one example:
... where the grid might have 10,000 cells. Most linked lists provided by standard libraries aren't optimized to store 10,000+ linked lists in the size of a 32-bit index per list since they're trying to provide interfaces that allow the linked list to be used in isolation (not using a separate backing data structure for storage like an array). Typically the most efficient use of a linked list is one that doesn't own memory or manage any resources. It's just linking data in an auxiliary fashion already allocated and managed in another data structure, like this for a 128-bit (16-byte) tree node in an n-ary tree where elements can be stored at any level of the hierarchy:
struct TreeNode
{
int32 parent; // parent index or -1 for no parent
int32 first_child; // first child of this node or -1
int32 next_sibling; // next child for the parent node or -1
int32 element; // element data stored in this node or -1
// if no data is associated
};
So there's a lot of use cases for implementing your own linked lists and other linked structures which are significantly more efficient for a more narrowly-applicable use case (grid data structures, octrees, quad-trees, graphs, etc), but again, I don't think you can use this trick in languages that don't easily allow you to utilize two or more levels of pointer indirection. Python inherently only has one for objects -- same with Java and C#. You'd need something like a "reference to a reference to an object" or an "index to an index to an object" or "an index to an object reference to an object".
Also linked lists generally aren't so useful in languages that don't allow you to manage where everything is stored in memory since you can end up getting cache misses galore iterating through linked lists otherwise if each list node is fragmented in memory as would often be the case after a GC cycle, e.g. For linked lists to be really efficient as in the case of the Linux kernel requires you to be able to really have fine control over where each node resides in memory so that list traversal is actually mostly, if not entirely, just iterating through contiguous chunks of memory. Otherwise you're generally better-off using small arrays even if that implies linear-time removals and insertions to/from the middle.
head
, Rather, a pointer-to-pointer is used instead. And fwiw, the samples posted will both "leak" the removed-node if nothing else points to it in the given example. They will be unreachable once the link is severed. – WhozCraig