Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to efficiently find the start node of a loop within a singly linked list, in Java?

/*
	This is an implementation that shows how
	to efficiently find the node from which 
	a loop originates in a singly linked list 
	that contains one.
	
	Let n be the number of nodes in the list.
	Time complexity: O(n) 
	Space complexity: O(1)
*/
public class FindLoop {
	private ListNode head;

	public FindLoop() {
		/*
		 * Create below linked list
		 * 0 -> 1 -> 2 -> 3 -> 4 -> 2
		 */
		head = new ListNode(0, null);
		ListNode loopNode = null, prev = head, temp;
		for (int i = 1; i <= 4; i++) {
			temp = new ListNode(i, null);
			if (i == 2) {
				loopNode = temp;
			}
			prev.next = temp;
			prev = temp;
		}
		prev.next = loopNode;
	}

	public static void main(String[] args) {
		FindLoop application = new FindLoop();
		ListNode middleNode = application.findLoopNode();
		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 findLoopNode() {
		ListNode slowPtr = head, fastPtr = head;
		// Loop until the fastPtr reaches the slowPtr
		while (fastPtr != null && fastPtr.next != null) {
			slowPtr = slowPtr.next;
			fastPtr = fastPtr.next.next;
			if (slowPtr == fastPtr) {
				break;
			}
		}
		// Reset the slowPtr to head
		slowPtr = head;
		// Loop until they cross again.
		while (slowPtr != fastPtr) {
			slowPtr = slowPtr.next;
			fastPtr = fastPtr.next;
		}

		return fastPtr;
	}

	// 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 :: java boucle for 
Java :: comparacion de strings java 
Java :: java hashmap time complexity 
Java :: java compute sum and average of array elements 
Java :: float.compare java 
Java :: android java show hide keyboard: 
Java :: static class java 
Java :: java pointer 
Java :: is it possible to quick sort a string in java 
Java :: add int to list java 
Java :: uninstall java 
Java :: main method in java 
Java :: java set operations 
Java :: pass array to method java 
Java :: java how to know if there is something on arguments 
Java :: android convert date to local timezone 
Java :: java for item in array 
Java :: how to convert errorBody to pojo in retrofit 
Java :: text field mouse event java 
Java :: android gridview item click effect ripple 
Java :: generate random number in java within a range without repeating with android studio 
Java :: finding length of arrays in java 
Java :: how to covert array into a char 
Java :: find the unique element in a list java 
Java :: square star pattern in java 
Java :: android java how to stop activity from opening twice programatically 
Java :: maths.abs function in java 
Java :: remove selected text from string value android java 
Java :: how to sort arraylist in java 
Java :: javax.persistence.noresultexception: no entity found for query 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =