File tree 1 file changed +10
-35
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +10
-35
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import com .fishercoder .common .classes .ListNode ;
4
4
5
- /**
6
- * 876. Middle of the Linked List
7
- *
8
- * Given a non-empty, singly linked list with head node head, return a middle node of linked list.
9
- *
10
- * If there are two middle nodes, return the second middle node.
11
- *
12
- * Example 1:
13
- *
14
- * Input: [1,2,3,4,5]
15
- * Output: Node 3 from this list (Serialization: [3,4,5])
16
- * The returned node has value 3. (The judge's serialization of this node is [3,4,5]).
17
- * Note that we returned a ListNode object ans, such that:
18
- * ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL.
19
- *
20
- * Example 2:
21
- *
22
- * Input: [1,2,3,4,5,6]
23
- * Output: Node 4 from this list (Serialization: [4,5,6])
24
- * Since the list has two middle nodes with values 3 and 4, we return the second one.
25
- *
26
- * Note:
27
- *
28
- * The number of nodes in the given list will be between 1 and 100.
29
- */
30
5
public class _876 {
31
- public static class Solution1 {
32
- public ListNode middleNode (ListNode head ) {
33
- ListNode fast = head ;
34
- ListNode slow = head ;
35
- while (fast != null && fast .next != null ) {
36
- fast = fast .next .next ;
37
- slow = slow .next ;
38
- }
39
- return slow ;
6
+ public static class Solution1 {
7
+ public ListNode middleNode (ListNode head ) {
8
+ ListNode fast = head ;
9
+ ListNode slow = head ;
10
+ while (fast != null && fast .next != null ) {
11
+ fast = fast .next .next ;
12
+ slow = slow .next ;
13
+ }
14
+ return slow ;
15
+ }
40
16
}
41
- }
42
17
}
You can’t perform that action at this time.
0 commit comments