Skip to content

Instantly share code, notes, and snippets.

@riyafa
Created September 3, 2020 00:28
Show Gist options
  • Save riyafa/44e97d59f89f38c99798e871473d3bf6 to your computer and use it in GitHub Desktop.
Save riyafa/44e97d59f89f38c99798e871473d3bf6 to your computer and use it in GitHub Desktop.
[Leetcode] 876. Middle of the Linked List
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