Created
March 11, 2023 07:20
-
-
Save VallarasuS/007cbccad996c9db1daea6a3029a4601 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 node = head; | |
ListNode[] nodes = new ListNode[100]; | |
int index = 0; | |
while(node != null) { | |
nodes[index++] = node; | |
node = node.next; | |
} | |
return nodes[index / 2]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment