Reverse a sublist in linked list

Here given code implementation process.

// C Program 
// Reverse a sublist in linked list
#include <stdio.h>
#include <stdlib.h> //for malloc function

// Linked List LinkNode
struct LinkNode
{
	int data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	int size;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
	// Create memory of head and tail Nodes
	struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
	if (sll == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		sll->head = NULL;
		sll->size = 0;
	}
	return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(int data)
{
	// Create dynamic node
	struct LinkNode *node = (struct LinkNode *) malloc(sizeof(struct LinkNode));
	if (node == NULL)
	{
		printf("Memory overflow\n");
	}
	else
	{
		// Set initial node value
		node->data = data;
		node->next = NULL;
	}
	return node;
}
// Add new Node at end of linked list 
void addNode(struct SingleLL *sll, int data)
{
	struct LinkNode *node = createLinkNode(data);
	if (sll->head == NULL)
	{
		sll->head = node;
		sll->size++;
	}
	else
	{
		struct LinkNode *temp = sll->head;
		// Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		// Append the node at last position
		temp->next = node;
		sll->size++;
	}
}
// Display linked list element
void display(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	struct LinkNode *temp = sll->head;
	// iterating linked list elements
	while (temp != NULL)
	{
		if (temp != sll->head)
		{
			printf("→");
		}
		printf(" %d ", temp->data);
		// Visit to next node
		temp = temp->next;
	}
	printf("→ NULL\n");
}
// Perform the operation of reverse sublist 
void reverseSubList(struct SingleLL *sll, int start, int last)
{
	// Define some useful variables that control execution process
	struct LinkNode *result = NULL;
	struct LinkNode *auxiliary = sll->head;
	struct LinkNode *back = NULL;
	struct LinkNode *current = NULL;
	struct LinkNode *first = NULL;
	// This is useful to detecting sublist element
	int count = 0;
	// iterating linked list element
	while (auxiliary != NULL && count <= last)
	{
		if (count >= start)
		{
			// Collect sublist element
			current = auxiliary;
			auxiliary = auxiliary->next;
			current->next = result;
			result = current;
			if (count == start)
			{
				// First node of sublist
				first = current;
			}
		}
		else
		{
			back = auxiliary;
			auxiliary = auxiliary->next;
		}
		count++;
	}
	if (first != NULL)
	{
		// Connect first part
		first->next = auxiliary;
	}
	if (back == NULL && result != NULL)
	{
		// When first node is part of sublist 
		// set new head
		sll->head = result;
	}
	else
	{
		// Connect second part
		back->next = result;
	}
}
// Handles the request of reversing sublist 
void reverseList(struct SingleLL *sll, int start, int last)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	else if (start < 0 || start >= sll->size || last < 0 || last >= sll->size)
	{
		// When sublist position are not valid
		return;
	}
	else if (last == start)
	{
		// No modifications are required
		return;
	}
	printf("\n Before reverse sublist (%d,%d) \n", start, last);
	// Display linked list
	display(sll);
	// Perform reverse operation
	reverseSubList(sll, start, last);
	printf(" After reverse sublist (%d,%d) \n", start, last);
	// Display linked list
	display(sll);
}
int main()
{
	// Create a empty linked list
	struct SingleLL *sll = newLinkedList();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	addNode(sll, 1);
	addNode(sll, 2);
	addNode(sll, 3);
	addNode(sll, 4);
	addNode(sll, 5);
	addNode(sll, 6);
	// Test
	reverseList(sll, 0, 3);
	reverseList(sll, 4, 5);
	reverseList(sll, 0, 5);
	return 0;
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
/*
  Java Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
};
public class SingleLL
{
	public LinkNode head;
	public int size;
	public SingleLL()
	{
		this.head = null;
		this.size = 0;
	}
	//Add new Node at end of linked list 
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
			this.size = 1;
		}
		else
		{
			LinkNode temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
			this.size++;
		}
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				System.out.print(" →");
			}
			System.out.print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" → NULL\n");
	}
	// Perform the operation of reverse sublist 
	public void reverseSubList(int start, int last)
	{
		// Define some useful variables that control execution process
		LinkNode result = null;
		LinkNode auxiliary = this.head;
		LinkNode back = null;
		LinkNode current = null;
		LinkNode first = null;
		// This is useful to detecting sublist element
		int count = 0;
		// iterating linked list element
		while (auxiliary != null && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary.next;
				current.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary.next;
			}
			count++;
		}
		if (first != null)
		{
			// Connect first part
			first.next = auxiliary;
		}
		if (back == null && result != null)
		{
			// When first node is part of sublist 
			// set new head
			this.head = result;
		}
		else
		{
			// Connect second part
			back.next = result;
		}
	}
	// Handles the request of reversing sublist 
	public void reverseList(int start, int last)
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		else if (start < 0 || start >= this.size || last < 0 || last >= this.size)
		{
			// When sublist position are not valid
			return;
		}
		else if (last == start)
		{
			// No modifications are required
			return;
		}
		System.out.print("\n Before reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		display();
		// Perform reverse operation
		reverseSubList(start, last);
		System.out.print(" After reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		display();
	}
	public static void main(String[] args)
	{
		// Create a empty linked list
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → NULL
		sll.addNode(1);
		sll.addNode(2);
		sll.addNode(3);
		sll.addNode(4);
		sll.addNode(5);
		sll.addNode(6);
		// Test
		sll.reverseList(0, 3);
		sll.reverseList(4, 5);
		sll.reverseList(0, 5);
	}
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
// Include header file
#include <iostream>
using namespace std;

/*
  C++ Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode
{
	public: int data;
	LinkNode *next;
	LinkNode(int data)
	{
		this->data = data;
		this->next = NULL;
	}
};;
class SingleLL
{
	public: LinkNode *head;
	int size;
	SingleLL()
	{
		this->head = NULL;
		this->size = 0;
	}
	//Add new Node at end of linked list
	void addNode(int data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
			this->size = 1;
		}
		else
		{
			LinkNode *temp = this->head;
			//Find last node
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			// Append the node at last position
			temp->next = node;
			this->size++;
		}
	}
	// Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		LinkNode *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			if (temp != this->head)
			{
				cout << " →";
			}
			cout << " " << temp->data;
			// Visit to next node
			temp = temp->next;
		}
		cout << " → NULL\n";
	}
	// Perform the operation of reverse sublist
	void reverseSubList(int start, int last)
	{
		// Define some useful variables that control execution process
		LinkNode *result = NULL;
		LinkNode *auxiliary = this->head;
		LinkNode *back = NULL;
		LinkNode *current = NULL;
		LinkNode *first = NULL;
		// This is useful to detecting sublist element
		int count = 0;
		// iterating linked list element
		while (auxiliary != NULL && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary->next;
				current->next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary->next;
			}
			count++;
		}
		if (first != NULL)
		{
			// Connect first part
			first->next = auxiliary;
		}
		if (back == NULL && result != NULL)
		{
			// When first node is part of sublist
			// set new head
			this->head = result;
		}
		else
		{
			// Connect second part
			back->next = result;
		}
	}
	// Handles the request of reversing sublist
	void reverseList(int start, int last)
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= this->size || last < 0 || last >= this->size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		cout << "\n Before reverse sublist (" << start << "," << last << ") \n";
		// Display linked list
		this->display();
		// Perform reverse operation
		this->reverseSubList(start, last);
		cout << " After reverse sublist (" << start << "," << last << ") \n";
		// Display linked list
		this->display();
	}
};
int main()
{
	// Create a empty linked list
	SingleLL sll = SingleLL();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1);
	sll.addNode(2);
	sll.addNode(3);
	sll.addNode(4);
	sll.addNode(5);
	sll.addNode(6);
	// Test
	sll.reverseList(0, 3);
	sll.reverseList(4, 5);
	sll.reverseList(0, 5);
	return 0;
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
// Include namespace system
using System;
/*
  C# Program for
  Reverse a sublist in linked list
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
	}
};
public class SingleLL
{
	public LinkNode head;
	public int size;
	public SingleLL()
	{
		this.head = null;
		this.size = 0;
	}
	//Add new Node at end of linked list
	public void addNode(int data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
			this.size = 1;
		}
		else
		{
			LinkNode temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
			this.size++;
		}
	}
	// Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				Console.Write(" →");
			}
			Console.Write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" → NULL\n");
	}
	// Perform the operation of reverse sublist
	public void reverseSubList(int start, int last)
	{
		// Define some useful variables that control execution process
		LinkNode result = null;
		LinkNode auxiliary = this.head;
		LinkNode back = null;
		LinkNode current = null;
		LinkNode first = null;
		// This is useful to detecting sublist element
		int count = 0;
		// iterating linked list element
		while (auxiliary != null && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary.next;
				current.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary.next;
			}
			count++;
		}
		if (first != null)
		{
			// Connect first part
			first.next = auxiliary;
		}
		if (back == null && result != null)
		{
			// When first node is part of sublist
			// set new head
			this.head = result;
		}
		else
		{
			// Connect second part
			back.next = result;
		}
	}
	// Handles the request of reversing sublist
	public void reverseList(int start, int last)
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= this.size || last < 0 || last >= this.size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		Console.Write("\n Before reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		display();
		// Perform reverse operation
		reverseSubList(start, last);
		Console.Write(" After reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		display();
	}
	public static void Main(String[] args)
	{
		// Create a empty linked list
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → NULL
		sll.addNode(1);
		sll.addNode(2);
		sll.addNode(3);
		sll.addNode(4);
		sll.addNode(5);
		sll.addNode(6);
		// Test
		sll.reverseList(0, 3);
		sll.reverseList(4, 5);
		sll.reverseList(0, 5);
	}
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
<?php
/*
  Php Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

	function __construct($data)
	{
		$this->data = $data;
		$this->next = null;
	}
};
class SingleLL
{
	public $head;
	public $size;

	function __construct()
	{
		$this->head = null;
		$this->size = 0;
	}
	//Add new Node at end of linked list
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
			$this->size = 1;
		}
		else
		{
			$temp = $this->head;
			//Find last node
			while ($temp->next != null)
			{
				$temp = $temp->next;
			}
			// Append the node at last position
			$temp->next = $node;
			$this->size++;
		}
	}
	// Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			if ($temp != $this->head)
			{
				echo " →";
			}
			echo " ". $temp->data;
			// Visit to next node
			$temp = $temp->next;
		}
		echo " → NULL\n";
	}
	// Perform the operation of reverse sublist
	public	function reverseSubList($start, $last)
	{
		// Define some useful variables that control execution process
		$result = null;
		$auxiliary = $this->head;
		$back = null;
		$current = null;
		$first = null;
		// This is useful to detecting sublist element
		$count = 0;
		// iterating linked list element
		while ($auxiliary != null && $count <= $last)
		{
			if ($count >= $start)
			{
				// Collect sublist element
				$current = $auxiliary;
				$auxiliary = $auxiliary->next;
				$current->next = $result;
				$result = $current;
				if ($count == $start)
				{
					// First node of sublist
					$first = $current;
				}
			}
			else
			{
				$back = $auxiliary;
				$auxiliary = $auxiliary->next;
			}
			$count++;
		}
		if ($first != null)
		{
			// Connect first part
			$first->next = $auxiliary;
		}
		if ($back == null && $result != null)
		{
			// When first node is part of sublist
			// set new head
			$this->head = $result;
		}
		else
		{
			// Connect second part
			$back->next = $result;
		}
	}
	// Handles the request of reversing sublist
	public	function reverseList($start, $last)
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		// When sublist position are not valid
		else if ($start < 0 || $start >= $this->size || $last < 0 || $last >= $this->size)
		{
			return;
		}
		// No modifications are required
		else if ($last == $start)
		{
			return;
		}
		echo "\n Before reverse sublist (". $start .",". $last .") \n";
		// Display linked list
		$this->display();
		// Perform reverse operation
		$this->reverseSubList($start, $last);
		echo " After reverse sublist (". $start .",". $last .") \n";
		// Display linked list
		$this->display();
	}
}

function main()
{
	// Create a empty linked list
	$sll = new SingleLL();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	$sll->addNode(1);
	$sll->addNode(2);
	$sll->addNode(3);
	$sll->addNode(4);
	$sll->addNode(5);
	$sll->addNode(6);
	// Test
	$sll->reverseList(0, 3);
	$sll->reverseList(4, 5);
	$sll->reverseList(0, 5);
}
main();

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
/*
  Node Js Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
};
class SingleLL
{
	constructor()
	{
		this.head = null;
		this.size = 0;
	}
	//Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
			this.size = 1;
		}
		else
		{
			var temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
			this.size++;
		}
	}
	// Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				process.stdout.write(" →");
			}
			process.stdout.write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" → NULL\n");
	}
	// Perform the operation of reverse sublist
	reverseSubList(start, last)
	{
		// Define some useful variables that control execution process
		var result = null;
		var auxiliary = this.head;
		var back = null;
		var current = null;
		var first = null;
		// This is useful to detecting sublist element
		var count = 0;
		// iterating linked list element
		while (auxiliary != null && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary.next;
				current.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary.next;
			}
			count++;
		}
		if (first != null)
		{
			// Connect first part
			first.next = auxiliary;
		}
		if (back == null && result != null)
		{
			// When first node is part of sublist
			// set new head
			this.head = result;
		}
		else
		{
			// Connect second part
			back.next = result;
		}
	}
	// Handles the request of reversing sublist
	reverseList(start, last)
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= this.size || last < 0 || last >= this.size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		process.stdout.write("\n Before reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
		// Perform reverse operation
		this.reverseSubList(start, last);
		process.stdout.write(" After reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
	}
}

function main()
{
	// Create a empty linked list
	var sll = new SingleLL();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1);
	sll.addNode(2);
	sll.addNode(3);
	sll.addNode(4);
	sll.addNode(5);
	sll.addNode(6);
	// Test
	sll.reverseList(0, 3);
	sll.reverseList(4, 5);
	sll.reverseList(0, 5);
}
main();

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
#   Python 3 Program for
#   Reverse a sublist in linked list

#  Linked list node
class LinkNode :
	
	def __init__(self, data) :
		self.data = data
		self.next = None
	

class SingleLL :
	
	def __init__(self) :
		self.head = None
		self.size = 0
	
	# Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
			self.size = 1
		else :
			temp = self.head
			# Find last node
			while (temp.next != None) :
				temp = temp.next
			
			#  Append the node at last position
			temp.next = node
			self.size += 1
		
	
	#  Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			if (temp != self.head) :
				print(" →", end = "")
			
			print("", temp.data, end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" → NULL")
	
	#  Perform the operation of reverse sublist 
	def reverseSubList(self, start, last) :
		#  Define some useful variables that control execution process
		result = None
		auxiliary = self.head
		back = None
		current = None
		first = None
		#  This is useful to detecting sublist element
		count = 0
		#  iterating linked list element
		while (auxiliary != None and count <= last) :
			if (count >= start) :
				#  Collect sublist element
				current = auxiliary
				auxiliary = auxiliary.next
				current.next = result
				result = current
				if (count == start) :
					#  First node of sublist
					first = current
				
			else :
				back = auxiliary
				auxiliary = auxiliary.next
			
			count += 1
		
		if (first != None) :
			#  Connect first part
			first.next = auxiliary
		
		if (back == None and result != None) :
			#  When first node is part of sublist 
			#  set new head
			self.head = result
		else :
			#  Connect second part
			back.next = result
		
	
	#  Handles the request of reversing sublist 
	def reverseList(self, start, last) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		elif(start < 0 or start >= self.size or last < 0 or last >= self.size) :
			#  When sublist position are not valid
			return
		
		elif(last == start) :
			#  No modifications are required
			return
		
		print("\n Before reverse sublist (", start ,",", last ,") ")
		#  Display linked list
		self.display()
		#  Perform reverse operation
		self.reverseSubList(start, last)
		print(" After reverse sublist (", start ,",", last ,") ")
		#  Display linked list
		self.display()
	

def main() :
	#  Create a empty linked list
	sll = SingleLL()
	#   Constructed linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1)
	sll.addNode(2)
	sll.addNode(3)
	sll.addNode(4)
	sll.addNode(5)
	sll.addNode(6)
	#  Test
	sll.reverseList(0, 3)
	sll.reverseList(4, 5)
	sll.reverseList(0, 5)

if __name__ == "__main__": main()

Output

 Before reverse sublist ( 0 , 3 )
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist ( 0 , 3 )
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist ( 4 , 5 )
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist ( 4 , 5 )
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist ( 0 , 5 )
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist ( 0 , 5 )
 5 → 6 → 1 → 2 → 3 → 4 → NULL
#   Ruby Program for
#   Reverse a sublist in linked list

#  Linked list node
class LinkNode  
	# Define the accessor and reader of class LinkNode  
	attr_reader :data, :next
	attr_accessor :data, :next
 
	
	def initialize(data) 
		self.data = data
		self.next = nil
	end

end

class SingleLL  
	# Define the accessor and reader of class SingleLL  
	attr_reader :head, :size
	attr_accessor :head, :size
 
	
	def initialize() 
		self.head = nil
		self.size = 0
	end

	# Add new Node at end of linked list 
	def addNode(data) 
		node = LinkNode.new(data)
		if (self.head == nil) 
			self.head = node
			self.size = 1
		else 
			temp = self.head
			# Find last node
			while (temp.next != nil) 
				temp = temp.next
			end

			#  Append the node at last position
			temp.next = node
			self.size += 1
		end

	end

	#  Display linked list element
	def display() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		temp = self.head
		# iterating linked list elements
		while (temp != nil) 
			if (temp != self.head) 
				print(" →")
			end

			print(" ", temp.data)
			#  Visit to next node
			temp = temp.next
		end

		print(" → NULL\n")
	end

	#  Perform the operation of reverse sublist 
	def reverseSubList(start, last) 
		#  Define some useful variables that control execution process
		result = nil
		auxiliary = self.head
		back = nil
		current = nil
		first = nil
		#  This is useful to detecting sublist element
		count = 0
		#  iterating linked list element
		while (auxiliary != nil && count <= last) 
			if (count >= start) 
				#  Collect sublist element
				current = auxiliary
				auxiliary = auxiliary.next
				current.next = result
				result = current
				if (count == start) 
					#  First node of sublist
					first = current
				end

			else 
				back = auxiliary
				auxiliary = auxiliary.next
			end

			count += 1
		end

		if (first != nil) 
			#  Connect first part
			first.next = auxiliary
		end

		if (back == nil && result != nil) 
			#  When first node is part of sublist 
			#  set new head
			self.head = result
		else 
			#  Connect second part
			back.next = result
		end

	end

	#  Handles the request of reversing sublist 
	def reverseList(start, last) 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		elsif(start < 0 || start >= self.size || last < 0 || last >= self.size) 
			#  When sublist position are not valid
			return
		elsif(last == start) 
			#  No modifications are required
			return
		end

		print("\n Before reverse sublist (", start ,",", last ,") \n")
		#  Display linked list
		self.display()
		#  Perform reverse operation
		self.reverseSubList(start, last)
		print(" After reverse sublist (", start ,",", last ,") \n")
		#  Display linked list
		self.display()
	end

end

def main() 
	#  Create a empty linked list
	sll = SingleLL.new()
	#   Constructed linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1)
	sll.addNode(2)
	sll.addNode(3)
	sll.addNode(4)
	sll.addNode(5)
	sll.addNode(6)
	#  Test
	sll.reverseList(0, 3)
	sll.reverseList(4, 5)
	sll.reverseList(0, 5)
end

main()

Output

 Before reverse sublist (0,3) 
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3) 
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5) 
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5) 
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5) 
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5) 
 5 → 6 → 1 → 2 → 3 → 4 → NULL
/*
  Scala Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
	def this(data: Int)
	{
		this(data, null);
	}
};
class SingleLL(var head: LinkNode , var size: Int)
{
	def this()
	{
		this(null, 0);
	}
	//Add new Node at end of linked list
	def addNode(data: Int): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
			this.size = 1;
		}
		else
		{
			var temp: LinkNode = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
			this.size += 1;
		}
	}
	// Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				print(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// Perform the operation of reverse sublist
	def reverseSubList(start: Int, last: Int): Unit = {
		// Define some useful variables that control execution process
		var result: LinkNode = null;
		var auxiliary: LinkNode = this.head;
		var back: LinkNode = null;
		var current: LinkNode = null;
		var first: LinkNode = null;
		// This is useful to detecting sublist element
		var count: Int = 0;
		// iterating linked list element
		while (auxiliary != null && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary.next;
				current.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary.next;
			}
			count += 1;
		}
		if (first != null)
		{
			// Connect first part
			first.next = auxiliary;
		}
		if (back == null && result != null)
		{
			// When first node is part of sublist
			// set new head
			this.head = result;
		}
		else
		{
			// Connect second part
			back.next = result;
		}
	}
	// Handles the request of reversing sublist
	def reverseList(start: Int, last: Int): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= this.size || last < 0 || last >= this.size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		print("\n Before reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
		// Perform reverse operation
		this.reverseSubList(start, last);
		print(" After reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create a empty linked list
		var sll: SingleLL = new SingleLL();
		//  Constructed linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → NULL
		sll.addNode(1);
		sll.addNode(2);
		sll.addNode(3);
		sll.addNode(4);
		sll.addNode(5);
		sll.addNode(6);
		// Test
		sll.reverseList(0, 3);
		sll.reverseList(4, 5);
		sll.reverseList(0, 5);
	}
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL
/*
  Swift 4 Program for
  Reverse a sublist in 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 size: Int;
	init()
	{
		self.head = nil;
		self.size = 0;
	}
	//Add new Node at end of linked list
	func addNode(_ data: Int)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
			self.size = 1;
		}
		else
		{
			var temp: LinkNode? = self.head;
			//Find last node
			while (temp!.next  != nil)
			{
				temp = temp!.next;
			}
			// Append the node at last position
			temp!.next = node;
			self.size += 1;
		}
	}
	// 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)
		{
			if (!(temp === self.head))
			{
				print(" →", terminator: "");
			}
			print("", temp!.data, terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" → NULL");
	}
	// Perform the operation of reverse sublist
	func reverseSubList(_ start: Int, _ last: Int)
	{
		// Define some useful variables that control execution process
		var result: LinkNode? = nil;
		var auxiliary: LinkNode? = self.head;
		var back: LinkNode? = nil;
		var current: LinkNode? = nil;
		var first: LinkNode? = nil;
		// This is useful to detecting sublist element
		var count: Int = 0;
		// iterating linked list element
		while (auxiliary  != nil && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary!.next;
				current!.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary!.next;
			}
			count += 1;
		}
		if (first  != nil)
		{
			// Connect first part
			first!.next = auxiliary;
		}
		if (back == nil && result  != nil)
		{
			// When first node is part of sublist
			// set new head
			self.head = result;
		}
		else
		{
			// Connect second part
			back!.next = result;
		}
	}
	// Handles the request of reversing sublist
	func reverseList(_ start: Int, _ last: Int)
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= self.size || last < 0 || last >= self.size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		print("\n Before reverse sublist (", start ,",", last ,") ");
		// Display linked list
		self.display();
		// Perform reverse operation
		self.reverseSubList(start, last);
		print(" After reverse sublist (", start ,",", last ,") ");
		// Display linked list
		self.display();
	}
}
func main()
{
	// Create a empty linked list
	let sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1);
	sll.addNode(2);
	sll.addNode(3);
	sll.addNode(4);
	sll.addNode(5);
	sll.addNode(6);
	// Test
	sll.reverseList(0, 3);
	sll.reverseList(4, 5);
	sll.reverseList(0, 5);
}
main();

Output

 Before reverse sublist ( 0 , 3 )
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist ( 0 , 3 )
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist ( 4 , 5 )
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist ( 4 , 5 )
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist ( 0 , 5 )
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist ( 0 , 5 )
 5 → 6 → 1 → 2 → 3 → 4 → NULL
/*
  Kotlin Program for
  Reverse a sublist in linked list
*/
// Linked list node
class LinkNode
{
	var data: Int;
	var next: LinkNode ? ;
	constructor(data: Int)
	{
		this.data = data;
		this.next = null;
	}
};
class SingleLL
{
	var head: LinkNode ? ;
	var size: Int;
	constructor()
	{
		this.head = null;
		this.size = 0;
	}
	//Add new Node at end of linked list
	fun addNode(data: Int): Unit
	{
		var node: LinkNode ? = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
			this.size = 1;
		}
		else
		{
			var temp: LinkNode ? = this.head;
			//Find last node
			while (temp?.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp?.next = node;
			this.size += 1;
		}
	}
	// Display linked list element
	fun display(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				print(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// Perform the operation of reverse sublist
	fun reverseSubList(start: Int, last: Int): Unit
	{
		// Define some useful variables that control execution process
		var result: LinkNode ? = null;
		var auxiliary: LinkNode ? = this.head;
		var back: LinkNode ? = null;
		var current: LinkNode ? ;
		var first: LinkNode ? = null;
		// This is useful to detecting sublist element
		var count: Int = 0;
		// iterating linked list element
		while (auxiliary != null && count <= last)
		{
			if (count >= start)
			{
				// Collect sublist element
				current = auxiliary;
				auxiliary = auxiliary.next;
				current.next = result;
				result = current;
				if (count == start)
				{
					// First node of sublist
					first = current;
				}
			}
			else
			{
				back = auxiliary;
				auxiliary = auxiliary.next;
			}
			count += 1;
		}
		if (first != null)
		{
			// Connect first part
			first.next = auxiliary;
		}
		if (back == null && result != null)
		{
			// When first node is part of sublist
			// set new head
			this.head = result;
		}
		else
		{
			// Connect second part
			back?.next = result;
		}
	}
	// Handles the request of reversing sublist
	fun reverseList(start: Int, last: Int): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// When sublist position are not valid
		else if (start < 0 || start >= this.size || last < 0 || last >= this.size)
		{
			return;
		}
		// No modifications are required
		else if (last == start)
		{
			return;
		}
		print("\n Before reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
		// Perform reverse operation
		this.reverseSubList(start, last);
		print(" After reverse sublist (" + start + "," + last + ") \n");
		// Display linked list
		this.display();
	}
}
fun main(args: Array < String > ): Unit
{
	// Create a empty linked list
	var sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → NULL
	sll.addNode(1);
	sll.addNode(2);
	sll.addNode(3);
	sll.addNode(4);
	sll.addNode(5);
	sll.addNode(6);
	// Test
	sll.reverseList(0, 3);
	sll.reverseList(4, 5);
	sll.reverseList(0, 5);
}

Output

 Before reverse sublist (0,3)
 1 → 2 → 3 → 4 → 5 → 6 → NULL
 After reverse sublist (0,3)
 4 → 3 → 2 → 1 → 5 → 6 → NULL

 Before reverse sublist (4,5)
 4 → 3 → 2 → 1 → 5 → 6 → NULL
 After reverse sublist (4,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL

 Before reverse sublist (0,5)
 4 → 3 → 2 → 1 → 6 → 5 → NULL
 After reverse sublist (0,5)
 5 → 6 → 1 → 2 → 3 → 4 → NULL

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







© 2021, kalkicode.com, All rights reserved