Skip to content

Instantly share code, notes, and snippets.

@VallarasuS
Created March 11, 2023 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VallarasuS/19b0e46f1035821a8b085ae1d167e092 to your computer and use it in GitHub Desktop.
Save VallarasuS/19b0e46f1035821a8b085ae1d167e092 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode middle = head;
ListNode end = head;
while(end != null && end.next != null) {
middle = middle.next;
end = end.next.next;
}
return middle;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment