Middle Node of a Linked List in JavaIn this section, we will learn about finding the middle node of a linked list in Java. We will also explore the various ways to find the middle node. Given: The first node or the Head of the Linked List is given (it is 14 in our case) 14 -> 16 -> 17 -> 11 -> 15 -> 18 -> 13 -> 12 -> NULL Return: 15 Given: 13 -> 17 -> 90 -> 76 -> 45 -> 32 -> 10 -> NULL Return: 76 Approach 1: Using two pointersIn this approach, we will use two pointers, one is fast (moves two steps at a time), and another is slow (moves one step at a time). It is obvious that the fast pointer reaches the end of the list before the two-pointers. When the fast pointer reaches the end of the linked list, then we check the position of the slow pointer that is moving one step at a time. The node at which the slow pointer is pointing in the middle node of the linked list. FileName: TwoPointerExample1.java Output: The middle node of the linked list is: 15 Approach 2: Using StackIn this approach, we will use a stack to find the middle node of the linked list. First, we will find the total size of the linked list. Now, we will push all of the nodes of the linked list in the stack. Then, we divide the total size by 2, and then whatever number comes, we do the pop operation on stack till that number of times. The top or peek node present in the stack is our answer; the middle node. FileName: StackExample1.java Output: The middle node of the linked list is: 76 Approach 3: Using QueueIn this approach, we will use a queue to find the middle node of the linked list. First, we will find the total size of the linked list. Now, we will push all of the nodes of the linked list in the queue. Then, we divide the total size by 2, and then whatever number comes, we do the removal of nodes from the queue till that number of times. The top or peek node present in the queue is our answer; the middle node. FileName: QueueExample1.java Output: The middle node of the linked list is: 76 Approach 4: Using one pointerIn this approach, we will use only one pointer to find the middle node of the linked list. First, we will find the total size of the linked list. Then, we divide the total size by 2, and then whatever number comes, we move the pointer, starting from the head node, to that number of times. The node at which the pointer is pointing is the middle node of the linked list. FileName: OnePointerExample1.java Output: The middle node of the linked list is: 76 Approach 5: Using CounterIn this approach also, we will use two pointers to find the middle node of the linked list with the help of a counter. We will keep incrementing the counter by 1 each time we move to the next node. When the value of the counter is odd, we will move the other pointer (say odd pointer) starting from the beginning (head node). After the end of the iteration, the position at which the odd pointer is pointing; is the middle node.' FileName: CounterExample.java Output: The middle node of the linked list is: 76
Next TopicPernicious Number in Java
|