-2

How to remove the first node of a linked list and add to a new linked list in java? I know removing and adding but, I'm confused how to transfer the data element from one node to the other.

Senku
  • 33
  • 1
  • 8

1 Answers1

0

This sounds like a school assignment, so I won't give you the actual code, but here is an overview of what you need to implement:

  • Let's name your two linked lists A and B (I'm going to assume you're dealing with singly linked lists). You want to remove the first node of A and add it to the end of B
  • First, you need to find the tail (last) node of B. It should have a field called next or something of that sort.
  • Make B.tail.next point to the location that A.head references (the first node of A)
  • Make A.head point to the location that A.head.next references (aka the second node of A)
  • Set B.tail.next to NULL (keep in mind that B.tail has now been updated to point to the old first node of A).

That's it! Make sure you do these in the right order, or you may overwrite a pointer that you need.

v0rtex20k
  • 1,041
  • 10
  • 20
  • @LakshmanPalli Yeah the site is a little tough in the beginning, but it gets better. Also, if you think my answer is suitable, can you please accept it? (green checkmark) – v0rtex20k Feb 12 '21 at 16:07
  • 1
    Yes, thank you, fella. I should dive in more and a lot to cover. – Senku Jul 07 '21 at 07:24