Created
April 12, 2020 16:03
-
-
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/
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(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