We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent d7e5a5f commit 1025c60Copy full SHA for 1025c60
Java/middle-of-the-linked-list.java
@@ -0,0 +1,26 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+class Solution {
12
+ public ListNode middleNode(ListNode head) {
13
+ ListNode currentNode = head;
14
+
15
+ ListNode slowPtr = head;
16
+ ListNode fastPtr = head;
17
18
+ while(fastPtr != null && fastPtr.next != null){
19
+ slowPtr = slowPtr.next; // slow pointer will move 1 node at a time
20
+ fastPtr = fastPtr.next.next; // fast pointer will move 2 nodes at a time
21
+ }
22
23
+ // at end slow pointer will be pointing to middle node
24
+ return slowPtr;
25
26
+}
0 commit comments