Deleting nth value in list of nodes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Deleting nth value in list of nodes

Where would I go to better understand how to delete the nth cause I’m a list of nodes? Was given a 4 minute video to show how to create a list and now, “Build a linked list of 20 nodes using the addNode function you created in Problem 1. Write a function to delete the kth node of the list. k is any number between 1 and 19.” I can Chegg it, but I wanna understand this. Do the teachers get better? :,)

13th Apr 2022, 8:44 PM
Mason Haines
8 Answers
0
Just iterate through the list while counting. Stop at the nth node, take the next reference of that node and add it to the previous node. It's like a chain of metal rings. You remove one and then connect the next one to the previous one, to reconnect the chain.
14th Apr 2022, 6:50 AM
Mustafa A
Mustafa A - avatar
+ 1
Yah that entire program is still far beyond anything i can comprehend. Thank you so much though, i have a better concept of this all
16th Apr 2022, 3:21 AM
Mason Haines
0
Thank you for that! So i gave it a try and now its telling me that i gave too many iterations? Tried deleting 1-2 and same thing.
15th Apr 2022, 2:43 PM
Mason Haines
0
Mason Haines How did you iterate?
15th Apr 2022, 2:54 PM
Mustafa A
Mustafa A - avatar
15th Apr 2022, 8:43 PM
Mason Haines
0
Heres the full code
15th Apr 2022, 8:43 PM
Mason Haines
0
Mason Haines You need to pass the first node (head) to the Delete function, not currentNode as it's implement to always point to the last element (tail). I would also recommend that you implement it as a class. I fixed it here if there is anything unclear. https://code.sololearn.com/cgbXpeMj79ck/?ref=app
15th Apr 2022, 9:56 PM
Mustafa A
Mustafa A - avatar
0
I just made a bare bones and generic implementation of LinkedList. My C++ is rusty but it should be helpful. https://code.sololearn.com/cJR941XcGV4s/?ref=app
16th Apr 2022, 12:11 AM
Mustafa A
Mustafa A - avatar