Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

need help with linked list  RSS feed

 
Darren Wilham
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am having trouble understanding how d1.next = d2.next, d2.next = d1, and n.next = d2 work?

I don't understand what the variables on the left are referencing when they are moving to the next node.

If I have a linked list like n -> [1| ] -> [2| ] -> [3| ] -> [4| ] -> [5| ] -> [6|null ]

1) d1 points to [2| ]
2) d2 points to [3| ]
3) ??? d2.next points to [4| ], but d1.next points to [3| ], I don't know what d1.next = d2.next would be?


 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

There have been several questions about linked lists in the last week or so. Have a look at this thread, which has a diagram of a linked list in. It’s the thread where I thought it was a doubly‑linked list and it was only singly‑linked. See whether that diagram helps. Draw it on paper, with a large gap between nodes, then insert a node in the gap, with its arrows in a different colour. See whether that helps you understand the workings. You will also realise that things to do with linked lists are easier if you can divide them into smaller parts. Similary for diagrams. Start with a diagram for singly‑linked lists before you try anything for doubly‑linked lists.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!