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.
Asked
Active
Viewed 70 times
-2
-
1Post your existing code and explain what it's doing and what you're having difficulty about. – user202729 Feb 12 '21 at 14:49
1 Answers
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
andB
(I'm going to assume you're dealing with singly linked lists). You want to remove the first node ofA
and add it to the end ofB
- First, you need to find the tail (last) node of
B
. It should have a field callednext
or something of that sort. - Make
B.tail.next
point to the location thatA.head
references (the first node of A) - Make
A.head
point to the location thatA.head.next
references (aka the second node of A) - Set
B.tail.next
to NULL (keep in mind thatB.tail
has now been updated to point to the old first node ofA
).
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