|
| 1 | +/* |
| 2 | + * LEETCODE 876 |
| 3 | +Given the head of a singly linked list, return the middle node of the linked list. |
| 4 | +If there are two middle nodes, return the second middle node. |
| 5 | +*Example 1: |
| 6 | +Input: head = [1,2,3,4,5] |
| 7 | +Output: [3,4,5] |
| 8 | +Explanation: The middle node of the list is node 3. |
| 9 | +
|
| 10 | +*Example 2: |
| 11 | +Input: head = [1,2,3,4,5,6] |
| 12 | +Output: [4,5,6] |
| 13 | +Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. |
| 14 | +
|
| 15 | +*CODE EXPLAINATION WITH DRY RUN: |
| 16 | +This Java code finds the midpoint node of a singly-linked list. If the linked list has an even number |
| 17 | +of nodes, it returns the second middle node. |
| 18 | +
|
| 19 | +First, the Node class is defined with a constructor that takes in an integer value and initializes |
| 20 | +the next reference to null. The insertAtTail() method takes in a head node and an integer value, |
| 21 | +and inserts a new node with the given value at the end of the linked list. The printLinkedList() method |
| 22 | +takes in a head node and prints out all the values in the linked list. |
| 23 | +
|
| 24 | +The makeLinkedList() method prompts the user to enter integers until -1 is inputted. Each integer is |
| 25 | +inserted into a new node at the tail of the linked list. The computeMidpoint() method takes in a head |
| 26 | +node and returns the middle node(s) of the linked list. If the linked list has no nodes or only one node, |
| 27 | +it just returns the head node. Otherwise, it initializes a slow pointer and a fast pointer to the head |
| 28 | +node. The while loop advances the fast pointer by two nodes and the slow pointer by one node at each |
| 29 | +iteration until the fast pointer reaches the end of the linked list. At that point, the slow pointer |
| 30 | +will be pointing to the midpoint node(s) of the linked list. |
| 31 | +
|
| 32 | +Finally, in the main() method, a new linked list is created by calling makeLinkedList(). The linked |
| 33 | +list is printed using printLinkedList(). The midpoint of the linked list is computed using |
| 34 | +computeMidpoint(), and its value is printed out. |
| 35 | +
|
| 36 | +*Example Dry Run: |
| 37 | +Suppose we have the following input: |
| 38 | +1 2 3 4 5 -1 |
| 39 | +This creates a linked list with the following structure: |
| 40 | +1 -> 2 -> 3 -> 4 -> 5 -> null |
| 41 | +Initially, the slow pointer and fast pointer both point to the head node, which is 1. In the first iteration of the while loop, the fast pointer moves two nodes ahead to node 3, while the slow pointer moves one node ahead to node 2. In the second iteration, the fast pointer moves another two nodes ahead to null, while the slow pointer moves one more node ahead to node 3. At this point, the slow pointer is pointing to the midpoint node(s) of the linked list. |
| 42 | +Therefore, computeMidpoint() returns node 3, which is printed out as output. |
| 43 | +
|
| 44 | +*/ |
| 45 | +import java.util.Scanner; |
| 46 | + |
| 47 | +public class linked_list_compute_midpoint { |
| 48 | + |
| 49 | + static class Node { |
| 50 | + |
| 51 | + int data; |
| 52 | + Node next; |
| 53 | + |
| 54 | + public Node(int data) { |
| 55 | + this.data = data; |
| 56 | + next = null; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + static Node insertAtTail(Node head, int data) { |
| 61 | + if (head == null) { |
| 62 | + head = new Node(data); |
| 63 | + return head; |
| 64 | + } |
| 65 | + Node n = new Node(data); |
| 66 | + Node temp = head; |
| 67 | + while (temp.next != null) { |
| 68 | + temp = temp.next; |
| 69 | + } |
| 70 | + temp.next = n; |
| 71 | + return head; |
| 72 | + } |
| 73 | + |
| 74 | + static void printLinkedList(Node head) { |
| 75 | + while (head != null) { |
| 76 | + System.out.print(head.data + "->"); |
| 77 | + head = head.next; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + static Node makeLinkedList() { |
| 82 | + Scanner scanner = new Scanner(System.in); |
| 83 | + int data = scanner.nextInt(); |
| 84 | + Node head = null; |
| 85 | + while (data != -1) { |
| 86 | + head = insertAtTail(head, data); |
| 87 | + data = scanner.nextInt(); |
| 88 | + } |
| 89 | + scanner.close(); |
| 90 | + return head; |
| 91 | + } |
| 92 | + |
| 93 | + static Node computeMidpoint(Node head) { |
| 94 | + if (head == null || head.next == null) { |
| 95 | + return head; |
| 96 | + } |
| 97 | + Node slow = head; |
| 98 | + Node fast = head.next; |
| 99 | + while (fast != null && fast.next != null) { |
| 100 | + fast = fast.next.next; |
| 101 | + slow = slow.next; |
| 102 | + } |
| 103 | + return slow; |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) { |
| 107 | + Node head = makeLinkedList(); |
| 108 | + printLinkedList(head); |
| 109 | + System.out.println(); |
| 110 | + Node midpoint = computeMidpoint(head); |
| 111 | + System.out.println(midpoint.data); |
| 112 | + } |
| 113 | +} |
0 commit comments