From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Delete the middle of a singly-linked list

Solution: Delete the middle of a singly-linked list - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Solution: Delete the middle of a singly-linked list

(upbeat music) - [Instructor] Let's delete the middle node of a singly-linked list in Java. The first step is to set up a list to test with, which is what we have here. You'll notice each item of the list is of type node. We created a node class as a part of the solution. It has two fields, a data field and a next node. The data field can be of any type, which we denote here using generics. We also have setters and getters for each attribute. Going back to our main class, we use a buildNode utility function to build out each node. The buildNode function creates a new node and sets the data appropriately, along with the next pointer to null. Technically, the next node reference is already null, but we set it here for clarity. Once the nodes are built, we configure them into a list. We make the head the first node of the list by connecting everything to it. The item after the head is nodeA, then nodeB, then nodeC, and we…

Contents