# Linked lists improved

**URL:** <https://forums.kodeco.com/t/linked-lists-improved/175901>\
**Category:** Data Structures & Algorithms in Dart\
**Created:** [July 29, 2022, 11:17pm UTC](https://forums.kodeco.com/t/linked-lists-improved/175901 "2022-07-29T23:17:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![warcayac](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warcayac/32/106713_2.png) [@warcayac](https://forums.kodeco.com/u/warcayac)\
**Post date:** [July 29, 2022, 11:17pm UTC](https://forums.kodeco.com/t/linked-lists-improved/175901/1 "2022-07-29T23:17:00Z")

</div>

I think the implementation of linked lists can be improved by…

- adding the **length** property,
- adding the **previous** property, which points at previous node, and
- replacing **insertAfter** and **removeAfter** methods with **insertAt** and **removeAt** methods respectively, which use the **index** parameter.

---

<div class="post-metadata">

**Author:** ![suragch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/suragch/32/109020_2.png) [@suragch](https://forums.kodeco.com/u/suragch)\
**Post date:** [August 22, 2022, 4:08pm UTC](https://forums.kodeco.com/t/linked-lists-improved/175901/2 "2022-08-22T16:08:52Z")

</div>

Those are some good ideas. Here are a few additional comments to take into consideration:

- **length** : The `length` property would have O(n) time complexity since you would need to start at `head` and count the nodes until you got to `tail`. You could improve this to O(1) if you added some additional logic to cache the length and update the value every time you added or removed a node.
- **previous** : Adding a `previous` property would make this a doubly linked list, which is a slightly different data structure than the singly linked list described in the Linked Lists chapter. The advantage of having `previous` is that it would be O(1) to get the previous node. The disadvantage is that the logic is a bit more complex and the memory overhead a little higher since you are pointing to an extra node. Everything is a tradeoff, so you need to make the decision based on the requirements of your application.
- **insert** : I completely agree that `insertAt` and `removeAt` with an index are much better from the UX perspective than `insertAfter` and `removeAfter`. The difference is that `insertAt` and `removeAt` are slower O(i) operations (where `i` is the index), whereas `insertAfter` and `removeAfter` are fast O(1) operations.

---

<div class="post-metadata">

**Author:** ![suragch](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/suragch/32/109020_2.png) [@suragch](https://forums.kodeco.com/u/suragch)\
**Post date:** [April 19, 2023, 5:44am UTC](https://forums.kodeco.com/t/linked-lists-improved/175901/3 "2023-04-19T05:44:53Z")

</div>

In the second edition of the book, I’m including a `DoublyLinkedList` implementation in the challenge folder for Chapter 5, “LinkedLists”. This has a `Node` with a `previous` property.
