Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are given a class SingleLinkedList implementing single-linked lists, which c

ID: 3839714 • Letter: Y

Question

You are given a class SingleLinkedList implementing single-linked lists, which contains a head data field referencing the list itself and an inner class Node which contains a data field data and a reference to the next node next. Write in pseudo-code or Java a method with signature: public void removeAdjacentDuplicates() that removes all adjacent nodes whose data field contain the same values. For example, the result of removing the adjacent duplicates in the list [1, 2, 2, 3, 4, 4, 4] should be [1, 2, 3, 4].

Explanation / Answer

Public void remove adjacent duplicates()

declare three pointers

Assign first pointer to the header node.

Traverse through the list checking when it reaches the end through the null pointer condition,picking one element at a time for comparison.

Until the end of the list is reached.

Assign the second pointer to the next node of the first pointer.

Compare the data of the two nodes.

If data is equal then delete the node pointed by the second pointer and update it to the new position.

Assign the second pointer to the third pointer

Update the second pointer by assining it to the next field pointed by itself

Update the first pointer by assignig its next field to the next field of the third pointer.

Delete the node pointed by the third pointer

else make the first and second pointers point to the next nodes respectively and repeat the procedure until the end of the list is reached.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote