Skip to content

Commit 6f7db29

Browse files
authored
Create MiddleOfTheLinkedList.js (codesONLY#83)
1 parent c13f1a7 commit 6f7db29

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

SDE_Sheet/MiddleOfTheLinkedList.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 slow = head;
14+
ListNode fast = head;
15+
16+
while(fast != null && fast.next != null){
17+
fast = fast.next.next;
18+
slow = slow.next;
19+
}
20+
return slow;
21+
}
22+
}

0 commit comments

Comments
 (0)