-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathN876_Middle_Linked_List.java
More file actions
31 lines (23 loc) · 939 Bytes
/
Copy pathN876_Middle_Linked_List.java
File metadata and controls
31 lines (23 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class N876_Middle_Linked_List {
public static ListNode middleNode(ListNode head) {
if (head.next == null) return head;
if (head.next.next == null) return head.next;
ListNode temp1 = head, temp2 = head;
while (temp2 != null && temp2.next != null) {
temp1 = temp1.next;
temp2 = temp2.next.next;
}
return temp1;
}
// public static void main(String[] args) {
// ListNode l5 = new ListNode(4);
// ListNode l4 = new ListNode(4,l5);
// ListNode l3 = new ListNode(3, l4);
// ListNode l2 = new ListNode(2, l3);
// ListNode l1 = new ListNode(1, l2);
//
// System.out.println(middleNode(l1).val);
// }
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Middle of the Linked List.
// Memory Usage: 41.5 MB, less than 49.99% of Java online submissions for Middle of the Linked List.