Created
September 3, 2020 00:28
-
-
Save riyafa/44e97d59f89f38c99798e871473d3bf6 to your computer and use it in GitHub Desktop.
[Leetcode] 876. Middle of the Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MiddleOfLinkedList { | |
public ListNode middleNode(ListNode head) { | |
ListNode slow = head; | |
ListNode fast = head; | |
while (fast != null && fast.next != null) { | |
slow = slow.next; | |
fast = fast.next.next; | |
} | |
return slow; | |
} | |
// Definition for singly-linked list. | |
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment