-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMiddleLinkedList.java
More file actions
62 lines (50 loc) · 1.89 KB
/
MiddleLinkedList.java
File metadata and controls
62 lines (50 loc) · 1.89 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class MiddleLinkedList {
public static void main(String[] args) {
ListNode head = new ListNode();
for(int i = 0; i < 10; i ++) {
ListNode cur = new ListNode(i);
add(cur,head);
}
ListNode ans = middleNode(head);
System.out.println(ans.val);
}
public static void add(ListNode cur,ListNode head) {
if(head.next == null) {
head.next = cur;
return;
}
ListNode temp = head;
while(temp.next!=null) {
temp = temp.next;
}
temp.next = cur;
}
public static ListNode middleNode(ListNode head) {
if(head.next == null) return head;
ListNode slow = head, fast = head;
while(fast != null && fast.next!=null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
}
//Solution: Using a slow pointer and a fast pointer, finding the middle of the list becomes easy. With incrementing the slow pointer by 1 and the
//fast pointer by 2. We Know that once the fast pointer is pointing to null that it reached the end of the list. So then we can just return the slow pointer
//The check in the while loop is important. fast!=null is a check for when the list is at odd length. fast.next!=null is a check for when the list is at even
//length. To make that make sense if fast pointer is incrementing by two and the current position = null. That means we went over the list and that can
//only happen when the list is at odd length. BIG O(n) run time complexity and O(1) space time complexity.
//One pass algorithm.