classSolution0x1{ // return secondary node while we got a even node linkedList public ListNode middleNode(ListNode head){ ListNode fast = head; ListNode slow = head; while (fast.next != null) { if (fast.next.next == null) fast = fast.next; else { fast = fast.next.next; slow = slow.next; } } return slow; } }
classSolution0x2{ public ListNode middleNode(ListNode head){ ListNode[] arr = new ListNode[100]; //we have restriction about length of linkedList int i = 0; int count = 0; while (head != null) { arr[i++] = head; count++; if (head.next == null) break; else head = head.next; } return arr[count / 2]; } }