Reverse each word in a linked list node

Here given code implementation process.

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

// Linked List LinkNode
struct LinkNode
{
	char data;
	struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
	struct LinkNode *head;
	struct LinkNode *tail;
};
// 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;
	}
	return sll;
}
// Returns a new Node of linked list
struct LinkNode *createLinkNode(char 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, char data)
{
	struct LinkNode *node = createLinkNode(data);
	if (sll->head == NULL)
	{
		sll->head = node;
	}
	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;
	}
}
// 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("→");
		}
		if (temp->data == ' ')
		{
			printf(" ' ' ");
		}
		else
		{
			printf(" %c ", temp->data);
		}
		// Visit to next node
		temp = temp->next;
	}
	printf("→ NULL\n");
}
void reverseWord(struct SingleLL *sll)
{
	if (sll->head == NULL)
	{
		printf("\n Empty linked list\n");
		return;
	}
	struct LinkNode *temp = sll->head;
	struct LinkNode *start = NULL;
	struct LinkNode *auxiliary = NULL;
	struct LinkNode *separator = NULL;
	struct LinkNode *hold = NULL;
	sll->head = NULL;
	// iterating linked list elements
	while (temp != NULL)
	{
		if (temp->data == ' ')
		{
			hold = temp;
			// Visit to next node
			temp = temp->next;
			// collect space ' '
			hold->next = separator;
			separator = hold;
			if (auxiliary != NULL)
			{
				auxiliary->next = NULL;
			}
			if (sll->head == NULL)
			{
				sll->head = start;
			}
			else if (start != NULL && auxiliary != NULL)
			{
				hold = separator;
				separator = hold->next;
				auxiliary->next = hold;
				hold->next = sll->head;
				sll->head = start;
			}
			start = NULL;
			auxiliary = NULL;
		}
		else
		{
			if (start == NULL)
			{
				start = temp;
			}
			auxiliary = temp;
			// Visit to next node
			temp = temp->next;
		}
	}
	// Manage last word
	if (separator != NULL)
	{
		hold = separator;
		// When more than two space exist
		while (hold->next != NULL)
		{
			hold = hold->next;
		}
		hold->next = sll->head;
		sll->head = separator;
	}
	if (auxiliary != NULL && start != NULL)
	{
		auxiliary->next = sll->head;
		sll->head = start;
	}
}
int main()
{
	// Create a empty linked list
	struct SingleLL *sll = newLinkedList();
	//  Constructed linked list
	//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	addNode(sll, 'T');
	addNode(sll, 'h');
	addNode(sll, 'i');
	addNode(sll, 's');
	addNode(sll, ' ');
	addNode(sll, 'I');
	addNode(sll, 's');
	addNode(sll, ' ');
	addNode(sll, 'G');
	addNode(sll, 'o');
	addNode(sll, 'o');
	addNode(sll, 'd');
	addNode(sll, ' ');
	addNode(sll, 'C');
	addNode(sll, 'o');
	addNode(sll, 'd');
	addNode(sll, 'e');
	display(sll);
	reverseWord(sll);
	display(sll);
	return 0;
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
/*
  Java Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	public char data;
	public LinkNode next;
	public LinkNode(char data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public SingleLL()
	{
		this.head = null;
	}
	//Add new Node at end of linked list 
	public void addNode(char data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			LinkNode temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// 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("→");
			}
			if (temp.data == ' ')
			{
				System.out.print(" ' ' ");
			}
			else
			{
				System.out.print(" " + temp.data + " ");
			}
			// Visit to next node
			temp = temp.next;
		}
		System.out.print("→ NULL\n");
	}
	// Reversing words in linked list
	public void reverseWord()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		// Define some auxiliary variables
		LinkNode temp = this.head;
		LinkNode start = null;
		LinkNode auxiliary = null;
		LinkNode separator = null;
		LinkNode hold = null;
		this.head = null;
		// iterating linked list elements
		while (temp != null)
		{
			if (temp.data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp.next;
				// collect space ' '
				hold.next = separator;
				separator = hold;
				if (auxiliary != null)
				{
					auxiliary.next = null;
				}
				if (this.head == null)
				{
					this.head = start;
				}
				else if (start != null && auxiliary != null)
				{
					// When more than one word exists
					hold = separator;
					separator = hold.next;
					auxiliary.next = hold;
					hold.next = this.head;
					this.head = start;
				}
				start = null;
				auxiliary = null;
			}
			else
			{
				if (start == null)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != null)
		{
			hold = separator;
			// When more than two space exist
			while (hold.next != null)
			{
				hold = hold.next;
			}
			hold.next = this.head;
			this.head = separator;
		}
		if (auxiliary != null && start != null)
		{
			auxiliary.next = this.head;
			this.head = start;
		}
	}
	public static void main(String[] args)
	{
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
		sll.addNode('T');
		sll.addNode('h');
		sll.addNode('i');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('I');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('G');
		sll.addNode('o');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode(' ');
		sll.addNode('C');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode('e');
		sll.display();
		sll.reverseWord();
		sll.display();
	}
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	public: 
    char data;
	LinkNode *next;
	LinkNode(char data)
	{
		this->data = data;
		this->next = NULL;
	}
};
class SingleLL
{
	public: 
    LinkNode *head;
	SingleLL()
	{
		this->head = NULL;
	}
	//Add new Node at end of linked list
	void addNode(char data)
	{
		LinkNode *node = new LinkNode(data);
		if (this->head == NULL)
		{
			this->head = node;
		}
		else
		{
			LinkNode *temp = this->head;
			//Find last node
			while (temp->next != NULL)
			{
				temp = temp->next;
			}
			// Append the node at last position
			temp->next = node;
		}
	}
	// 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 << "→";
			}
			if (temp->data == ' ')
			{
				cout << " ' ' ";
			}
			else
			{
				cout << " " << temp->data << " ";
			}
			// Visit to next node
			temp = temp->next;
		}
		cout << "→ NULL\n";
	}
	// Reversing words in linked list
	void reverseWord()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		// Define some auxiliary variables
		LinkNode *temp = this->head;
		LinkNode *start = NULL;
		LinkNode *auxiliary = NULL;
		LinkNode *separator = NULL;
		LinkNode *hold = NULL;
		this->head = NULL;
		// iterating linked list elements
		while (temp != NULL)
		{
			if (temp->data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp->next;
				// collect space ' '
				hold->next = separator;
				separator = hold;
				if (auxiliary != NULL)
				{
					auxiliary->next = NULL;
				}
				if (this->head == NULL)
				{
					this->head = start;
				}
				else if (start != NULL && auxiliary != NULL)
				{
					// When more than one word exists
					hold = separator;
					separator = hold->next;
					auxiliary->next = hold;
					hold->next = this->head;
					this->head = start;
				}
				start = NULL;
				auxiliary = NULL;
			}
			else
			{
				if (start == NULL)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp->next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != NULL)
		{
			hold = separator;
			// When more than two space exist
			while (hold->next != NULL)
			{
				hold = hold->next;
			}
			hold->next = this->head;
			this->head = separator;
		}
		if (auxiliary != NULL && start != NULL)
		{
			auxiliary->next = this->head;
			this->head = start;
		}
	}
};
int main()
{
	SingleLL sll = SingleLL();
	//  Constructed linked list
	//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	sll.addNode('T');
	sll.addNode('h');
	sll.addNode('i');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('I');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('G');
	sll.addNode('o');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode(' ');
	sll.addNode('C');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode('e');
	sll.display();
	sll.reverseWord();
	sll.display();
	return 0;
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
// Include namespace system
using System;
/*
  C# Program for
  Reverse each word in a linked list node
*/
// Linked list node
public class LinkNode
{
	public char data;
	public LinkNode next;
	public LinkNode(char data)
	{
		this.data = data;
		this.next = null;
	}
}
public class SingleLL
{
	public LinkNode head;
	public SingleLL()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	public void addNode(char data)
	{
		LinkNode node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			LinkNode temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// 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("→");
			}
			if (temp.data == ' ')
			{
				Console.Write(" ' ' ");
			}
			else
			{
				Console.Write(" " + temp.data + " ");
			}
			// Visit to next node
			temp = temp.next;
		}
		Console.Write("→ NULL\n");
	}
	// Reversing words in linked list
	public void reverseWord()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		// Define some auxiliary variables
		LinkNode temp = this.head;
		LinkNode start = null;
		LinkNode auxiliary = null;
		LinkNode separator = null;
		LinkNode hold = null;
		this.head = null;
		// iterating linked list elements
		while (temp != null)
		{
			if (temp.data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp.next;
				// collect space ' '
				hold.next = separator;
				separator = hold;
				if (auxiliary != null)
				{
					auxiliary.next = null;
				}
				if (this.head == null)
				{
					this.head = start;
				}
				else if (start != null && auxiliary != null)
				{
					// When more than one word exists
					hold = separator;
					separator = hold.next;
					auxiliary.next = hold;
					hold.next = this.head;
					this.head = start;
				}
				start = null;
				auxiliary = null;
			}
			else
			{
				if (start == null)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != null)
		{
			hold = separator;
			// When more than two space exist
			while (hold.next != null)
			{
				hold = hold.next;
			}
			hold.next = this.head;
			this.head = separator;
		}
		if (auxiliary != null && start != null)
		{
			auxiliary.next = this.head;
			this.head = start;
		}
	}
	public static void Main(String[] args)
	{
		SingleLL sll = new SingleLL();
		//  Constructed linked list
		//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
		sll.addNode('T');
		sll.addNode('h');
		sll.addNode('i');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('I');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('G');
		sll.addNode('o');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode(' ');
		sll.addNode('C');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode('e');
		sll.display();
		sll.reverseWord();
		sll.display();
	}
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
<?php
/*
  Php Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	public $data;
	public $next;

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

	function __construct()
	{
		$this->head = null;
	}
	//Add new Node at end of linked list
	public	function addNode($data)
	{
		$node = new LinkNode($data);
		if ($this->head == null)
		{
			$this->head = $node;
		}
		else
		{
			$temp = $this->head;
			//Find last node
			while ($temp->next != null)
			{
				$temp = $temp->next;
			}
			// Append the node at last position
			$temp->next = $node;
		}
	}
	// 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 "→";
			}
			if ($temp->data == ' ')
			{
				echo " ' ' ";
			}
			else
			{
				echo " ". $temp->data ." ";
			}
			// Visit to next node
			$temp = $temp->next;
		}
		echo "→ NULL\n";
	}
	// Reversing words in linked list
	public	function reverseWord()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		// Define some auxiliary variables
		$temp = $this->head;
		$start = null;
		$auxiliary = null;
		$separator = null;
		$hold = null;
		$this->head = null;
		// iterating linked list elements
		while ($temp != null)
		{
			if ($temp->data == ' ')
			{
				$hold = $temp;
				// Visit to next node
				$temp = $temp->next;
				// collect space ' '
				$hold->next = $separator;
				$separator = $hold;
				if ($auxiliary != null)
				{
					$auxiliary->next = null;
				}
				if ($this->head == null)
				{
					$this->head = $start;
				}
				else if ($start != null && $auxiliary != null)
				{
					// When more than one word exists
					$hold = $separator;
					$separator = $hold->next;
					$auxiliary->next = $hold;
					$hold->next = $this->head;
					$this->head = $start;
				}
				$start = null;
				$auxiliary = null;
			}
			else
			{
				if ($start == null)
				{
					$start = $temp;
				}
				$auxiliary = $temp;
				// Visit to next node
				$temp = $temp->next;
			}
		}
		// When no word exists
		// Or Manage last word
		if ($separator != null)
		{
			$hold = $separator;
			// When more than two space exist
			while ($hold->next != null)
			{
				$hold = $hold->next;
			}
			$hold->next = $this->head;
			$this->head = $separator;
		}
		if ($auxiliary != null && $start != null)
		{
			$auxiliary->next = $this->head;
			$this->head = $start;
		}
	}
}

function main()
{
	$sll = new SingleLL();
	//  Constructed linked list
	//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	$sll->addNode('T');
	$sll->addNode('h');
	$sll->addNode('i');
	$sll->addNode('s');
	$sll->addNode(' ');
	$sll->addNode('I');
	$sll->addNode('s');
	$sll->addNode(' ');
	$sll->addNode('G');
	$sll->addNode('o');
	$sll->addNode('o');
	$sll->addNode('d');
	$sll->addNode(' ');
	$sll->addNode('C');
	$sll->addNode('o');
	$sll->addNode('d');
	$sll->addNode('e');
	$sll->display();
	$sll->reverseWord();
	$sll->display();
}
main();

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
/*
  Node Js Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	constructor(data)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	constructor()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	addNode(data)
	{
		var node = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		else
		{
			var temp = this.head;
			//Find last node
			while (temp.next != null)
			{
				temp = temp.next;
			}
			// Append the node at last position
			temp.next = node;
		}
	}
	// 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("→");
			}
			if (temp.data == ' ')
			{
				process.stdout.write(" ' ' ");
			}
			else
			{
				process.stdout.write(" " + temp.data + " ");
			}
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write("→ NULL\n");
	}
	// Reversing words in linked list
	reverseWord()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		// Define some auxiliary variables
		var temp = this.head;
		var start = null;
		var auxiliary = null;
		var separator = null;
		var hold = null;
		this.head = null;
		// iterating linked list elements
		while (temp != null)
		{
			if (temp.data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp.next;
				// collect space ' '
				hold.next = separator;
				separator = hold;
				if (auxiliary != null)
				{
					auxiliary.next = null;
				}
				if (this.head == null)
				{
					this.head = start;
				}
				else if (start != null && auxiliary != null)
				{
					// When more than one word exists
					hold = separator;
					separator = hold.next;
					auxiliary.next = hold;
					hold.next = this.head;
					this.head = start;
				}
				start = null;
				auxiliary = null;
			}
			else
			{
				if (start == null)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != null)
		{
			hold = separator;
			// When more than two space exist
			while (hold.next != null)
			{
				hold = hold.next;
			}
			hold.next = this.head;
			this.head = separator;
		}
		if (auxiliary != null && start != null)
		{
			auxiliary.next = this.head;
			this.head = start;
		}
	}
}

function main()
{
	var sll = new SingleLL();
	//  Constructed linked list
	//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	sll.addNode('T');
	sll.addNode('h');
	sll.addNode('i');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('I');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('G');
	sll.addNode('o');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode(' ');
	sll.addNode('C');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode('e');
	sll.display();
	sll.reverseWord();
	sll.display();
}
main();

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
#   Python 3 Program for
#   Reverse each word in a linked list node

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

class SingleLL :
	
	def __init__(self) :
		self.head = None
	
	# Add new Node at end of linked list 
	def addNode(self, data) :
		node = LinkNode(data)
		if (self.head == None) :
			self.head = node
		else :
			temp = self.head
			# Find last node
			while (temp.next != None) :
				temp = temp.next
			
			#  Append the node at last position
			temp.next = node
		
	
	#  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 = "")
			
			if (temp.data == ' ') :
				print(" ' ' ", end = "")
			else :
				print("", temp.data ," ", end = "")
			
			#  Visit to next node
			temp = temp.next
		
		print("→ NULL")
	
	#  Reversing words in linked list
	def reverseWord(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		#  Define some auxiliary variables
		temp = self.head
		start = None
		auxiliary = None
		separator = None
		hold = None
		self.head = None
		#  iterating linked list elements
		while (temp != None) :
			if (temp.data == ' ') :
				hold = temp
				#  Visit to next node
				temp = temp.next
				#  collect space ' '
				hold.next = separator
				separator = hold
				if (auxiliary != None) :
					auxiliary.next = None
				
				if (self.head == None) :
					self.head = start
				
				elif(start != None and auxiliary != None) :
					#  When more than one word exists
					hold = separator
					separator = hold.next
					auxiliary.next = hold
					hold.next = self.head
					self.head = start
				
				start = None
				auxiliary = None
			else :
				if (start == None) :
					start = temp
				
				auxiliary = temp
				#  Visit to next node
				temp = temp.next
			
		
		#  When no word exists
		#  Or Manage last word
		if (separator != None) :
			hold = separator
			#  When more than two space exist
			while (hold.next != None) :
				hold = hold.next
			
			hold.next = self.head
			self.head = separator
		
		if (auxiliary != None and start != None) :
			auxiliary.next = self.head
			self.head = start
		
	

def main() :
	sll = SingleLL()
	#   Constructed linked list
	#   T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	sll.addNode('T')
	sll.addNode('h')
	sll.addNode('i')
	sll.addNode('s')
	sll.addNode(' ')
	sll.addNode('I')
	sll.addNode('s')
	sll.addNode(' ')
	sll.addNode('G')
	sll.addNode('o')
	sll.addNode('o')
	sll.addNode('d')
	sll.addNode(' ')
	sll.addNode('C')
	sll.addNode('o')
	sll.addNode('d')
	sll.addNode('e')
	sll.display()
	sll.reverseWord()
	sll.display()

if __name__ == "__main__": main()

Output

 T  → h  → i  → s  → ' ' → I  → s  → ' ' → G  → o  → o  → d  → ' ' → C  → o  → d  → e  → NULL
 C  → o  → d  → e  → ' ' → G  → o  → o  → d  → ' ' → I  → s  → ' ' → T  → h  → i  → s  → NULL
#   Ruby Program for
#   Reverse each word in a linked list node

#  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
	attr_accessor :head
 
	
	def initialize() 
		self.head = nil
	end

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

			#  Append the node at last position
			temp.next = node
		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

			if (temp.data == ' ') 
				print(" ' ' ")
			else 
				print(" ", temp.data ," ")
			end

			#  Visit to next node
			temp = temp.next
		end

		print("→ NULL\n")
	end

	#  Reversing words in linked list
	def reverseWord() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		#  Define some auxiliary variables
		temp = self.head
		start = nil
		auxiliary = nil
		separator = nil
		hold = nil
		self.head = nil
		#  iterating linked list elements
		while (temp != nil) 
			if (temp.data == ' ') 
				hold = temp
				#  Visit to next node
				temp = temp.next
				#  collect space ' '
				hold.next = separator
				separator = hold
				if (auxiliary != nil) 
					auxiliary.next = nil
				end

				if (self.head == nil) 
					self.head = start
				elsif(start != nil && auxiliary != nil) 
					#  When more than one word exists
					hold = separator
					separator = hold.next
					auxiliary.next = hold
					hold.next = self.head
					self.head = start
				end

				start = nil
				auxiliary = nil
			else 
				if (start == nil) 
					start = temp
				end

				auxiliary = temp
				#  Visit to next node
				temp = temp.next
			end

		end

		#  When no word exists
		#  Or Manage last word
		if (separator != nil) 
			hold = separator
			#  When more than two space exist
			while (hold.next != nil) 
				hold = hold.next
			end

			hold.next = self.head
			self.head = separator
		end

		if (auxiliary != nil && start != nil) 
			auxiliary.next = self.head
			self.head = start
		end

	end

end

def main() 
	sll = SingleLL.new()
	#   Constructed linked list
	#   T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	sll.addNode('T')
	sll.addNode('h')
	sll.addNode('i')
	sll.addNode('s')
	sll.addNode(' ')
	sll.addNode('I')
	sll.addNode('s')
	sll.addNode(' ')
	sll.addNode('G')
	sll.addNode('o')
	sll.addNode('o')
	sll.addNode('d')
	sll.addNode(' ')
	sll.addNode('C')
	sll.addNode('o')
	sll.addNode('d')
	sll.addNode('e')
	sll.display()
	sll.reverseWord()
	sll.display()
end

main()

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
/*
  Scala Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode(var data: Character , var next: LinkNode)
{
	def this(data: Char)
	{
		this(data, null);
	}
}
class SingleLL(var head: LinkNode)
{
	def this()
	{
		this(null);
	}
	//Add new Node at end of linked list
	def addNode(data: Char): Unit = {
		var node: LinkNode = new LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		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;
		}
	}
	// 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("→");
			}
			if (temp.data == ' ')
			{
				print(" ' ' ");
			}
			else
			{
				print(" " + temp.data + " ");
			}
			// Visit to next node
			temp = temp.next;
		}
		print("→ NULL\n");
	}
	// Reversing words in linked list
	def reverseWord(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// Define some auxiliary variables
		var temp: LinkNode = this.head;
		var start: LinkNode = null;
		var auxiliary: LinkNode = null;
		var separator: LinkNode = null;
		var hold: LinkNode = null;
		this.head = null;
		// iterating linked list elements
		while (temp != null)
		{
			if (temp.data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp.next;
				// collect space ' '
				hold.next = separator;
				separator = hold;
				if (auxiliary != null)
				{
					auxiliary.next = null;
				}
				if (this.head == null)
				{
					this.head = start;
				}
				else if (start != null && auxiliary != null)
				{
					// When more than one word exists
					hold = separator;
					separator = hold.next;
					auxiliary.next = hold;
					hold.next = this.head;
					this.head = start;
				}
				start = null;
				auxiliary = null;
			}
			else
			{
				if (start == null)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != null)
		{
			hold = separator;
			// When more than two space exist
			while (hold.next != null)
			{
				hold = hold.next;
			}
			hold.next = this.head;
			this.head = separator;
		}
		if (auxiliary != null && start != null)
		{
			auxiliary.next = this.head;
			this.head = start;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var sll: SingleLL = new SingleLL();
		//  Constructed linked list
		//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
		sll.addNode('T');
		sll.addNode('h');
		sll.addNode('i');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('I');
		sll.addNode('s');
		sll.addNode(' ');
		sll.addNode('G');
		sll.addNode('o');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode(' ');
		sll.addNode('C');
		sll.addNode('o');
		sll.addNode('d');
		sll.addNode('e');
		sll.display();
		sll.reverseWord();
		sll.display();
	}
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → NULL
/*
  Swift 4 Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	var data: Character;
	var next: LinkNode? ;
	init(_ data: Character)
	{
		self.data = data;
		self.next = nil;
	}
}
class SingleLL
{
	var head: LinkNode? ;
	init()
	{
		self.head = nil;
	}
	//Add new Node at end of linked list
	func addNode(_ data: Character)
	{
		let node: LinkNode? = LinkNode(data);
		if (self.head == nil)
		{
			self.head = node;
		}
		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;
		}
	}
	// 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: "");
			}
			if (temp!.data == " ")
			{
				print("' '", terminator: "");
			}
			else
			{
				print("", temp!.data ,"", terminator: "");
			}
			// Visit to next node
			temp = temp!.next;
		}
		print("→ NULL");
	}
	// Reversing words in linked list
	func reverseWord()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		// Define some auxiliary variables
		var temp: LinkNode? = self.head;
		var start: LinkNode? = nil;
		var auxiliary: LinkNode? = nil;
		var separator: LinkNode? = nil;
		var hold: LinkNode? = nil;
		self.head = nil;
		// iterating linked list elements
		while (temp  != nil)
		{
			if (temp!.data == " ")
			{
				hold = temp;
				// Visit to next node
				temp = temp!.next;
				// collect space " "
				hold!.next = separator;
				separator = hold;
				if (auxiliary  != nil)
				{
					auxiliary!.next = nil;
				}
				if (self.head == nil)
				{
					self.head = start;
				}
				else if (start  != nil && auxiliary  != nil)
				{
					// When more than one word exists
					hold = separator;
					separator = hold!.next;
					auxiliary!.next = hold;
					hold!.next = self.head;
					self.head = start;
				}
				start = nil;
				auxiliary = nil;
			}
			else
			{
				if (start == nil)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp!.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator  != nil)
		{
			hold = separator;
			// When more than two space exist
			while (hold!.next  != nil)
			{
				hold = hold!.next;
			}
			hold!.next = self.head;
			self.head = separator;
		}
		if (auxiliary  != nil && start  != nil)
		{
			auxiliary!.next = self.head;
			self.head = start;
		}
	}
}
func main()
{
	let sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  T → h → i → s →" " → I → s → " " → G → o → o → d → " " → C → o → d → e → NULL
	sll.addNode("T");
	sll.addNode("h");
	sll.addNode("i");
	sll.addNode("s");
	sll.addNode(" ");
	sll.addNode("I");
	sll.addNode("s");
	sll.addNode(" ");
	sll.addNode("G");
	sll.addNode("o");
	sll.addNode("o");
	sll.addNode("d");
	sll.addNode(" ");
	sll.addNode("C");
	sll.addNode("o");
	sll.addNode("d");
	sll.addNode("e");
	sll.display();
	sll.reverseWord();
	sll.display();
}
main();

Output

 T → h → i → s →' '→ I → s →' '→ G → o → o → d →' '→ C → o → d → e → NULL
 C → o → d → e →' '→ G → o → o → d →' '→ I → s →' '→ T → h → i → s → NULL
/*
  Kotlin Program for
  Reverse each word in a linked list node
*/
// Linked list node
class LinkNode
{
	var data: Char;
	var next: LinkNode ? ;
	constructor(data: Char)
	{
		this.data = data;
		this.next = null;
	}
}
class SingleLL
{
	var head: LinkNode ? ;
	constructor()
	{
		this.head = null;
	}
	//Add new Node at end of linked list
	fun addNode(data: Char): Unit
	{
		var node: LinkNode ? = LinkNode(data);
		if (this.head == null)
		{
			this.head = node;
		}
		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;
		}
	}
	// 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("→");
			}
			if (temp.data == ' ')
			{
				print(" ' ' ");
			}
			else
			{
				print(" " + temp.data + " ");
			}
			// Visit to next node
			temp = temp.next;
		}
		print("→ NULL\n");
	}
	// Reversing words in linked list
	fun reverseWord(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		// Define some auxiliary variables
		var temp: LinkNode ? = this.head;
		var start: LinkNode ? = null;
		var auxiliary: LinkNode ? = null;
		var separator: LinkNode ? = null;
		var hold: LinkNode?;
		this.head = null;
		// iterating linked list elements
		while (temp != null)
		{
			if (temp.data == ' ')
			{
				hold = temp;
				// Visit to next node
				temp = temp.next;
				// collect space ' '
				hold.next = separator;
				separator = hold;
				if (auxiliary != null)
				{
					auxiliary.next = null;
				}
				if (this.head == null)
				{
					this.head = start;
				}
				else if (start != null && auxiliary != null)
				{
					// When more than one word exists
					hold = separator;
					separator = hold.next;
					auxiliary.next = hold;
					hold.next = this.head;
					this.head = start;
				}
				start = null;
				auxiliary = null;
			}
			else
			{
				if (start == null)
				{
					start = temp;
				}
				auxiliary = temp;
				// Visit to next node
				temp = temp.next;
			}
		}
		// When no word exists
		// Or Manage last word
		if (separator != null)
		{
			hold = separator;
			// When more than two space exist
			while (hold?.next != null)
			{
				hold = hold.next;
			}
			hold?.next = this.head;
			this.head = separator;
		}
		if (auxiliary != null && start != null)
		{
			auxiliary.next = this.head;
			this.head = start;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var sll: SingleLL = SingleLL();
	//  Constructed linked list
	//  T → h → i → s →' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
	sll.addNode('T');
	sll.addNode('h');
	sll.addNode('i');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('I');
	sll.addNode('s');
	sll.addNode(' ');
	sll.addNode('G');
	sll.addNode('o');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode(' ');
	sll.addNode('C');
	sll.addNode('o');
	sll.addNode('d');
	sll.addNode('e');
	sll.display();
	sll.reverseWord();
	sll.display();
}

Output

 T → h → i → s → ' ' → I → s → ' ' → G → o → o → d → ' ' → C → o → d → e → NULL
 C → o → d → e → ' ' → G → o → o → d → ' ' → I → s → ' ' → T → h → i → s → 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