• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

need help with linked list

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

Posts: 76412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic