Learn-dsa..in 30 days!



























CC-7 : Partition/ re-arrange a SingleLinkedList around a given input number.

Description:

Given an input SingleLinkedList, and an input number N, re-arrange the SingleLinkedList, so that nodes with data value lesser than N occur before node with datavalue N in the SingleLinkedList and nodes with data value greater than N occur after N in the SingleLinkedList.

Test cases and expected outputs:

Input Parameters Expected outputs
8->44->7->3->11->2->17->15->12->19
Partition Value is : 15
8->7->3->11->2->12->15->44->17->19
8->44->7->3->11->2->17->11->12->19
Partition Value is : 2
2->8->44->7->3->11->17->11->12->19
8->44->7->3->11->2->17->11->12->19
Partition Value is : 44
8->7->3->11->2->17->11->12->19->44

Pseudocode:

We will partition the input SingleLinkedList into 3 separate SingleLinkedLists P1,P2and P3.
Traverse through each node of input SingleLinkedList:
If the currentNode’s data value is less than N, add currentNode to SIngleLinkedList P1.
If the currentNode’s data value is equal to N, add currentNode to SIngleLinkedList P2.
If the currentNode’s data value is greater than N, add currentNode to SIngleLinkedList P3.
Join P1 and P2 and P3.

Code:

public SingleLinkedList singleLinkedListPartition(SingleLinkedList singleLinkedList, int partition) throws Exception{
	if (singleLinkedList.getFirstNode()==null) { return singleLinkedList;}
	SingleLinkedListNode firstNodeP1=null; SingleLinkedListNode lastNodeP1=null;
	SingleLinkedListNode firstNodeP2=null; SingleLinkedListNode lastNodeP2=null;
	SingleLinkedListNode firstNodeP3=null; SingleLinkedListNode lastNodeP3=null;
	SingleLinkedListNode currentNode=singleLinkedList.getFirstNode();
	while (currentNode !=null) {
		if (currentNode.getNodeData() < partition) {
			if (firstNodeP1==null) {
				firstNodeP1=currentNode;
				lastNodeP1=currentNode;
			}else {
				lastNodeP1.setNextNode(currentNode);
				lastNodeP1=currentNode;
			}
		}else {if (currentNode.getNodeData() == partition) {
			if (firstNodeP2==null) {
				firstNodeP2=currentNode;
				lastNodeP2=currentNode;
			}else {
				lastNodeP2.setNextNode(currentNode);
				lastNodeP2=currentNode;
			}
			} else {	if (currentNode.getNodeData() > partition) {
				if (firstNodeP3==null) {
					firstNodeP3=currentNode;
					lastNodeP3=currentNode;
				}else {
					lastNodeP3.setNextNode(currentNode);
					lastNodeP3=currentNode;
				}	
			}
		}
		}
		currentNode=currentNode.getNextNode();
	}
	if (lastNodeP1 != null) {
		if (firstNodeP2 != null) {
			lastNodeP1.setNextNode(firstNodeP2);
			lastNodeP2.setNextNode(firstNodeP3);
			}else {
				lastNodeP1.setNextNode(firstNodeP3);
			}
	}else {if (lastNodeP2 != null) {
		lastNodeP2.setNextNode(firstNodeP3);
	}
	}		
	if (firstNodeP1!=null) {
		singleLinkedList.setFirstNode(firstNodeP1);
	}else { if (firstNodeP2 !=null) {
		singleLinkedList.setFirstNode(firstNodeP2);
	}else {
		singleLinkedList.setFirstNode(firstNodeP3);
	}
	}		
	return singleLinkedList;
}

Click here to download and run code and test cases !