Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find the middle node of a singly linked list without counting its nodes, in Java?

/*
	This is an implementation that shows how
	to efficiently get to the middle node 
	of a singly linked list whose size is not 
	known in advance.
	Each linked list node has an integer value
	as well as a next pointer that points to 
	the next node or null in case of tail of list.
	
	Let n be the number of nodes in the list.
	Time complexity: O(n) 
	Space complexity: O(1)
*/
public class MiddleNode {
	private ListNode head;
	public MiddleNode() {
		/*
		 * Create below linked list
		 * 0 -> 1 -> 2 -> 3 -> 4
		 */
		head = new ListNode(0, null);
		ListNode prev = head, temp;
		for (int i = 1; i <= 4; i++) {
			temp = new ListNode(i, null);
			prev.next = temp;
			prev = temp;
		}
	}
	public static void main(String[] args) {
		MiddleNode application = new MiddleNode();
		ListNode middleNode = application.findMiddleNode();
		System.out.println(middleNode.val); // 2
	}
	// Two pointers are maintained, namely fastPtr and slowPtr.
	// fastPtr advances by 2 steps every time
	// while the slow one by 1 step only.
	public ListNode findMiddleNode() {
		ListNode slowPtr = head, fastPtr = head;
		// By the time the fastPtr reaches the end of list
		// the slowPtr would reach the middle node
		while (fastPtr != null && fastPtr.next != null) {
			slowPtr = slowPtr.next;
			fastPtr = fastPtr.next.next;
		}
		return slowPtr;
	}
	// Class representing a linked list node
	// with pointers to value and next node
	private class ListNode {
		int val;
		ListNode next;
		ListNode(int val, ListNode next) {
			this.val = val;
			this.next = next;
		}
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: Panel Slider for java swing 
Java :: convert char to string java 
Java :: string to localdate in java 
Java :: set drawableleft programmatically android 
Java :: button background color not changing android 
Java :: java intercept ctrl c 
Java :: java how to center window 
Java :: jlabel on the center of a jpanel 
Java :: how to converet negative byte value to postive int value in java 
Java :: Date from String java3 
Java :: throw io exception java 
Java :: add oracle jdbc driver to eclipse java project 
Java :: java array contains 
Java :: javafx textarea new line with shift+enter 
Java :: input string a list in java 
Java :: how to change top of window in java 
Java :: android studio start activity 
Java :: react native android gif 
Java :: org.mockito.exceptions.misusing.WrongTypeOfReturnValue 
Java :: int to binary java 
Java :: javafx detect collision 
Java :: how to make a char uppercase in java 
Java :: how to change maven java version in po, 
Java :: hibernate create query count return 
Java :: java 8 group a collections by 2 property 
Java :: java number 
Java :: java stack using linked list 
Java :: java sout two dimensional array 
Java :: how to set spinner value in android 
Java :: java find time between two dates 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =