-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMiddleofLinkedList.java
More file actions
68 lines (61 loc) · 1.72 KB
/
MiddleofLinkedList.java
File metadata and controls
68 lines (61 loc) · 1.72 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
63
64
65
66
67
68
/*
MiddleofLinkedList.java
Middle of Linked List
Find the middle node of a linked list.
Example
Given 1->2->3, return the node with value 2.
Given 1->2, return the node with value 1.
Tags Linked List
*/
public class MiddleofLinkedList {
//Definition for ListNode
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
/**
* @param head: the head of linked list.
* @return: a middle node of the linked list
*/
public static ListNode middleNode(ListNode head) {
// Write your code here
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head.next;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public static void main(String[] args) {
MiddleofLinkedList test = new MiddleofLinkedList();
ListNode test1 = new ListNode(1);
ListNode test2 = new ListNode(2);
ListNode test3 = new ListNode(3);
ListNode test4 = new ListNode(4);
ListNode test5 = new ListNode(5);
ListNode test6 = new ListNode(6);
ListNode test7 = new ListNode(7);
ListNode test8 = new ListNode(8);
ListNode test9 = new ListNode(9);
test1.next = test2;
test2.next = test3;
test3.next = test4;
test4.next = test5;
test5.next = test6;
test6.next = test7;
test7.next = test8;
test8.next = test9;
test9.next = null;
ListNode result = test.middleNode(test1);
System.out.println("Expected: 5");
System.out.println(result.val);
}
}