-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.

1
  • 1
    Post your existing code and explain what it's doing and what you're having difficulty about. Commented Feb 12, 2021 at 14:49

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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)
Yes, thank you, fella. I should dive in more and a lot to cover.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.