Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- class Solution {
- public ListNode middleNode(ListNode head) {
- ListNode iterator = new ListNode(0);
- iterator.next = head;
- int count = 0 ;
- while(iterator.next != null){
- iterator = iterator.next;
- count++;
- }
- if(count == 1){
- return iterator;
- }
- count = (count/2) -1;
- iterator = head; //reset to head and iterate to middle integer
- for(int i = 0; i < count; i++){
- iterator = iterator.next;
- }
- return iterator.next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement