I know how to add nodes before and after the head and tail, but I dont know how to add a node to an empty doubly linked list. How would I go about doing this? Thank you.
-
2That depends on the implementation of a doubly-linked list that you're using -- Python doesn't come with one. But the sensible thing would be to add it exactly the way you would add a node to a non-empty list, except that now it doesn't matter whether you add it to the head or the tail.Katriel– Katriel2013年03月29日 12:47:35 +00:00Commented Mar 29, 2013 at 12:47
1 Answer 1
When inserting into an empty list, make both head
and tail
refer to the new node. Also, make sure that the node's next
and previous
references are consistent with what the rest of the code is expecting.
answered Mar 29, 2013 at 12:47
lang-py