Created
March 11, 2023 07:32
-
-
Save VallarasuS/19b0e46f1035821a8b085ae1d167e092 to your computer and use it in GitHub Desktop.
876. Middle of The Linked List - https://leetcode.com/problems/middle-of-the-linked-list/
This file contains 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
/** | |
* 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