Skip to main content

Rotate linked list clockwise by k nodes in swift

Rotating linked list node clockwise

Swift program for Rotate linked list clockwise by k nodes. Here mentioned other language solution.

import Foundation
/*
  Swift 4 program for 
  Rotate a list by moving each element 
  k times to the right.
  Or clockwise rotation of linked list 
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode? ;
	init(_ data: Int)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	var tail: LinkNode? ;
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	// Add new Node at end of linked list 
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		else
		{
			// Append the node at last position
			self.tail!.next = node;
		}
		self.tail = node;
	}
	// Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		// iterating linked list elements
		while (temp  != nil)
		{
			print("",temp!.data, terminator: " →");
			// Visit to next node
			temp = temp!.next;
		}
		print(" NULL");
	}
	// This are perform the linked list rotation
	func rotation(_ k: Int)
	{
		// Define some auxiliary variable
		var auxiliary: LinkNode? = self.head;
		var count: Int = 0;
		// Count number of node in linked list
		while (auxiliary  != nil)
		{
			// visit to next node
			auxiliary = auxiliary!.next;
			count += 1;
		}
		if (count <= 1)
		{
			// When have less than 2 nodes in linked list
			return;
		}
		// Get actual rotation
		count = k % count;
		if (count == 0)
		{
			// When actual linked list are not affected
			return;
		}
		auxiliary = self.head;
		// Find the rotational node
		while (count > 1)
		{
			// Visit to next node
			auxiliary = auxiliary!.next;
			count -= 1;
		}
		// Connecting the last node to first node of linked list
		self.tail!.next = self.head;
		// Set new last node
		self.tail = auxiliary;
		// Set new head
		self.head = auxiliary!.next;
		// Set that there is no node after of tail
		self.tail!.next = nil;
	}
	// Handles the request to perform rotation
	// k is indicate node rotation
	func clockwiseRotation(_ k: Int)
	{
		if (k <= 0)
		{
			// When invalid given k
			return;
		}
		if (self.head == nil)
		{
			print("\n Empty Linked List");
			return;
		}
		// Display given linked list
		print(" Before rotate ");
		print(" Linked List : ");
		self.display();
		print(" Given k :",k);
		// Perform rotation
		self.rotation(k);
		// Display resultant linked list
		print(" After rotate ");
		print(" Linked List : ");
		self.display();
		print("\n");
	}
	static func main()
	{
		let sll1: SingleLL = SingleLL();
		let sll2: SingleLL = SingleLL();
		// First linked list
		// 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
		sll1.addNode(1);
		sll1.addNode(2);
		sll1.addNode(3);
		sll1.addNode(4);
		sll1.addNode(5);
		sll1.addNode(6);
		sll1.addNode(7);
		sll1.addNode(8);
		// Second linked list
		// 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
		sll2.addNode(4);
		sll2.addNode(9);
		sll2.addNode(7);
		sll2.addNode(3);
		sll2.addNode(8);
		sll2.addNode(6);
		sll2.addNode(-2);
		// Test case
		sll1.clockwiseRotation(3);
		sll2.clockwiseRotation(18);
	}
}
SingleLL.main();

Output

 Before rotate
 Linked List :
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 Given k : 3
 After rotate
 Linked List :
 4 → 5 → 6 → 7 → 8 → 1 → 2 → 3 → NULL


 Before rotate
 Linked List :
 4 → 9 → 7 → 3 → 8 → 6 → -2 → NULL
 Given k : 18
 After rotate
 Linked List :
 8 → 6 → -2 → 4 → 9 → 7 → 3 → NULL





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment