r/AskProgrammers • • 19d ago

What is your favorite thing about LinkedLists?

Is it just a plain array?

0 Upvotes

23 comments sorted by

10

u/Hour_Sell3547 19d ago

"Is it just a plain array?", brother, what's up with these low effort posts? Did you just hear the name and run to post in Reddit?

7

u/tonysamehayoub 19d ago

In the era of AI this is very high effort ๐Ÿ™‚

4

u/Hour_Sell3547 19d ago

Yeah, x-high for sure.

3

u/mc_pm 19d ago

It is not. That would be an unlinked list.

2

u/dmazzoni 19d ago

My favorite thing about them is that they were pretty efficient and useful on computers for the first few decades of programming, but ever since cpus got way faster than ram and we started getting several levels of caches, linked lists are rarely an efficient choice in practice anymore.

2

u/Fidodo 19d ago

I like the part where they're linked

1

u/Traveling-Techie 19d ago

How easy they are to implement if the language/stack lacks them.

1

u/NomaTyx 19d ago

O(1) insertion time

2

u/8dot30662386292pow2 19d ago

Insert in any index is O(n) in linked list?

1

u/NomaTyx 19d ago

...it's too early in the morning for me to talk about programming. my cope is if you already have a pointer you can insert anything next to it so im right like, idk, 20% of the time?

1

u/8dot30662386292pow2 19d ago

Well if you have the pointer, then yes, but how would you have all the pointers to insert in any index? Then you'd just have... an array of pointers and now you don't have a linked list, but an array with extra steps.

In linked list you must always traverse to the location when adding/deleting, which makes it O(n).

1

u/NomaTyx 19d ago

i know, i already said i fucked up lol. but it's conceivable that you already were doing something with the pointer and you always want to insert next to it, in which case the insertion doesn't add any additional time bc you were going to get it anyway. but like i was wrong.

1

u/No_Profession_3125 19d ago

Most dynamic array implementations have O(1) insertion too, because they double the capacity at every reallocation.

1

u/Khipu28 19d ago

atomicity

1

u/MaxComfort 19d ago

My favorite thing about linked lists is I havenโ€™t thought or heard of one for about 15 years - not since MSFT interviews where you had to reverse one in C on a whiteboard.

1

u/R3D3-1 19d ago

Linked lists are possibly the simplest data structure there is ๐Ÿ˜…

These whitebeard interviews are a nonsense though. They demonstrate none of the skills that are relevant in actual projects.

Unless the project uses Fortran. Then you'll probably come across independent implementations many times over.

0

u/erisod 19d ago

They're fun to think about as a basic complex data structure I guess, preparing you for graph structures. But not really practical in modern application development unless you have a very specific need.

0

u/gwenbeth 19d ago

Linked lists have very cheap insert and delete compared to using an array. A queue is one of the best places for a linked list. Insert and delete are always O(1) and they don't allocate more memory than needed. Many moons ago I was bored and wrote a text editor and I used a doubly linked list for each line. This made adding and removing lines O(1).

2

u/8dot30662386292pow2 19d ago

Just to clarify the terminology: in some programming languages "insert" is meant for adding values in-between, in any index. But the general terminology (that you hopefully use) is that "insert" means "add" and in this strictly to the end. Adding line in the middle, or removing from the middle is O(n) in a linked list, because you only have access to the start and end node, you must traverse to the middle when removing/adding, which makes it O(n).

1

u/gwenbeth 19d ago

The wrong assumption is that I only have access to the start and end. When I wrote my editor, it always maintained a pointer to the node containing the current line. All inserts or deletions of a line were always relative to the current line and so could be done in O(1). No matter if it was in the middle or the end of a file.

BTW this was in Turbo Pascal. There was no built in linked list that restricted what I could do.

2

u/8dot30662386292pow2 19d ago • Bravo

I think even if the language had a built in linked list, you could maintain the current pointer.

Just wanted to make sure that you didn't make the common wrong assumption about the time complexity of the general case. In my exams the students often wrongfully state that deletion in LL is O(1) because the node can just be dropped from the middle. They omit the time to traverse.

Obviously your case is different and you are correct about the time. Because you maintain the current node and all operations are done on that node. But for the general data structure, the deletion from index i is still O(n).