Skip to content

Instantly share code, notes, and snippets.

@ozkansari
Created April 12, 2020 16:03
Show Gist options
  • Save ozkansari/b2e4e14e27d8dcfab51a47c0c299d297 to your computer and use it in GitHub Desktop.
Save ozkansari/b2e4e14e27d8dcfab51a47c0c299d297 to your computer and use it in GitHub Desktop.
Middle of the Linked List : Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middle node. https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class LinkedListMiddleNodeFastPointer {
public ListNode middleNode(ListNode head) {
ListNode sizePointer = head;
ListNode middlePointer = head;
while(sizePointer != null && sizePointer.next != null) {
middlePointer = middlePointer.next;
sizePointer = sizePointer.next.next;
}
return middlePointer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment