Fold Linked List

Here given code implementation process.

// C Program 
// Fold 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;
    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(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;
    }
    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(" →");
        }
        printf(" %d", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" → NULL\n");
}

// This is handle the request of folding given linked list nodes
void foldList(struct SingleLL *sll)
{
    if (sll->head == NULL)
    {
        printf("\n Empty linked list\n");
        return;
    }

    struct LinkNode *temp = sll->head;

    struct LinkNode *auxiliary = sll->head;

    struct LinkNode *hold = NULL;

    // Loop which Find middle node
    while (temp != NULL && temp->next != NULL && temp->next->next != NULL)
    { 
        
        auxiliary = auxiliary->next;
        
        temp = temp->next->next;
       
    }

    if(auxiliary != NULL)
    {

        hold = auxiliary;

        auxiliary = auxiliary->next;
        // Separating half nodes
        hold->next = NULL;

        // Start with first node of next half section
        temp = auxiliary;

        auxiliary = NULL;

        // Reverse second half nodes 
        while(temp != NULL)
        {
            hold = temp;

            temp = temp->next;

            hold->next = auxiliary;

            auxiliary = hold;
        }
        // Start with first node of linked list
        temp = sll->head;

        // Combine first and second half nodes in alternate manner
        while(temp!=NULL)
        {
            hold = temp;

            temp = temp->next;

            hold->next = auxiliary;

            if(auxiliary != NULL)
            {
                hold = auxiliary;

                auxiliary = auxiliary->next;

                hold->next = temp;
            }
        }   
    }

  
  
}
int main()
{
    // Create a empty linked lists
    struct SingleLL*sll1 = newLinkedList();
    struct SingleLL*sll2 = newLinkedList();
    //  Constructed first linked list
    //  1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL

    addNode( sll1, 1);
    addNode( sll1, 2);
    addNode( sll1, 3);
    addNode( sll1, 4);
    addNode( sll1, 5);
    addNode( sll1, 6);
    addNode( sll1, 7);
    addNode( sll1, 8);

    printf(" Before folding \n");
    display(sll1);
    // Form of folding
    //      1 → 2 → 3 → 4 ──┐
    // NULL ← 8 ← 7 ← 6 ← 5⤶
    foldList(sll1);
    // After merging
    // 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
    printf(" After folding \n");
    display(sll1);

    // Constructed second linked list
    //  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
    addNode( sll2, 1);
    addNode( sll2, 2);
    addNode( sll2, 3);
    addNode( sll2, 4);
    addNode( sll2, 5);
    addNode( sll2, 6);
    addNode( sll2, 7);

    printf(" Before folding \n");
    display(sll2);
    // Form of folding
    //      1 → 2 → 3 → 4 
    // NULL ← 7 ← 6 ← 5⤶
    foldList(sll2);
    // After merging
    // 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
    printf(" After folding \n");
    display(sll2);
    return 0;
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
/*
  Java Program
  Fold 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 SingleLL()
	{
		this.head = null;
	}
	//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;
		}
		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(" →");
			}
			System.out.print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		System.out.print(" → NULL\n");
	}
	// This is handle the request of folding given linked list nodes
	public void foldList()
	{
		if (this.head == null)
		{
			System.out.print("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		LinkNode auxiliary = this.head;
		LinkNode hold = null;
		// Loop which Find middle node
		while (temp != null && temp.next != null && temp.next.next != null)
		{
			auxiliary = auxiliary.next;
			temp = temp.next.next;
		}
		if (auxiliary != null)
		{
			hold = auxiliary;
			auxiliary = auxiliary.next;
			// Separating half nodes
			hold.next = null;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = null;
			// Reverse second half nodes 
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this.head;
			// Combine first and second half nodes in alternate manner
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				if (auxiliary != null)
				{
					hold = auxiliary;
					auxiliary = auxiliary.next;
					hold.next = temp;
				}
			}
		}
	}
	public static void main(String[] args)
	{
		// Create a empty linked lists
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		//  Constructed 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);
		System.out.print(" Before folding \n");
		sll1.display();
		// Form of folding
		//      1 → 2 → 3 → 4 ──┐
		// NULL ← 8 ← 7 ← 6 ← 5⤶
		sll1.foldList();
		// After merging
		// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
		System.out.print(" After folding \n");
		sll1.display();
		// Constructed second linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
		sll2.addNode(1);
		sll2.addNode(2);
		sll2.addNode(3);
		sll2.addNode(4);
		sll2.addNode(5);
		sll2.addNode(6);
		sll2.addNode(7);
		System.out.print(" Before folding \n");
		sll2.display();
		// Form of folding
		//      1 → 2 → 3 → 4 
		// NULL ← 7 ← 6 ← 5⤶
		sll2.foldList();
		// After merging
		// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
		System.out.print(" After folding \n");
		sll2.display();
	}
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program
  Fold 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;
	SingleLL()
	{
		this->head = NULL;
	}
	//Add new Node at end of linked list
	void addNode(int 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 << " →";
			}
			cout << " " << temp->data;
			// Visit to next node
			temp = temp->next;
		}
		cout << " → NULL\n";
	}
	// This is handle the request of folding given linked list nodes
	void foldList()
	{
		if (this->head == NULL)
		{
			cout << "\n Empty linked list\n";
			return;
		}
		LinkNode *temp = this->head;
		LinkNode *auxiliary = this->head;
		LinkNode *hold = NULL;
		// Loop which Find middle node
		while (temp != NULL && temp->next != NULL && temp->next->next != NULL)
		{
			auxiliary = auxiliary->next;
			temp = temp->next->next;
		}
		if (auxiliary != NULL)
		{
			hold = auxiliary;
			auxiliary = auxiliary->next;
			// Separating half nodes
			hold->next = NULL;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = NULL;
			// Reverse second half nodes
			while (temp != NULL)
			{
				hold = temp;
				temp = temp->next;
				hold->next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this->head;
			// Combine first and second half nodes in alternate manner
			while (temp != NULL)
			{
				hold = temp;
				temp = temp->next;
				hold->next = auxiliary;
				if (auxiliary != NULL)
				{
					hold = auxiliary;
					auxiliary = auxiliary->next;
					hold->next = temp;
				}
			}
		}
	}
};
int main()
{
	// Create a empty linked lists
	SingleLL sll1 = SingleLL();
	SingleLL sll2 = SingleLL();
	//  Constructed 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);
	cout << " Before folding \n";
	sll1.display();
	// Form of folding
	//      1 → 2 → 3 → 4 ──┐
	// NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList();
	// After merging
	// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	cout << " After folding \n";
	sll1.display();
	// Constructed second linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1);
	sll2.addNode(2);
	sll2.addNode(3);
	sll2.addNode(4);
	sll2.addNode(5);
	sll2.addNode(6);
	sll2.addNode(7);
	cout << " Before folding \n";
	sll2.display();
	// Form of folding
	//      1 → 2 → 3 → 4
	// NULL ← 7 ← 6 ← 5⤶
	sll2.foldList();
	// After merging
	// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	cout << " After folding \n";
	sll2.display();
	return 0;
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
// Include namespace system
using System;
/*
  C# Program
  Fold 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 SingleLL()
	{
		this.head = null;
	}
	//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;
		}
		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(" →");
			}
			Console.Write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		Console.Write(" → NULL\n");
	}
	// This is handle the request of folding given linked list nodes
	public void foldList()
	{
		if (this.head == null)
		{
			Console.Write("\n Empty linked list\n");
			return;
		}
		LinkNode temp = this.head;
		LinkNode auxiliary = this.head;
		LinkNode hold = null;
		// Loop which Find middle node
		while (temp != null && temp.next != null && temp.next.next != null)
		{
			auxiliary = auxiliary.next;
			temp = temp.next.next;
		}
		if (auxiliary != null)
		{
			hold = auxiliary;
			auxiliary = auxiliary.next;
			// Separating half nodes
			hold.next = null;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = null;
			// Reverse second half nodes
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this.head;
			// Combine first and second half nodes in alternate manner
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				if (auxiliary != null)
				{
					hold = auxiliary;
					auxiliary = auxiliary.next;
					hold.next = temp;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		// Create a empty linked lists
		SingleLL sll1 = new SingleLL();
		SingleLL sll2 = new SingleLL();
		//  Constructed 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);
		Console.Write(" Before folding \n");
		sll1.display();
		// Form of folding
		//      1 → 2 → 3 → 4 ──┐
		// NULL ← 8 ← 7 ← 6 ← 5⤶
		sll1.foldList();
		// After merging
		// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
		Console.Write(" After folding \n");
		sll1.display();
		// Constructed second linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
		sll2.addNode(1);
		sll2.addNode(2);
		sll2.addNode(3);
		sll2.addNode(4);
		sll2.addNode(5);
		sll2.addNode(6);
		sll2.addNode(7);
		Console.Write(" Before folding \n");
		sll2.display();
		// Form of folding
		//      1 → 2 → 3 → 4
		// NULL ← 7 ← 6 ← 5⤶
		sll2.foldList();
		// After merging
		// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
		Console.Write(" After folding \n");
		sll2.display();
	}
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
<?php
/*
  Php Program
  Fold Linked List
*/
// 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 " →";
			}
			echo " ". $temp->data;
			// Visit to next node
			$temp = $temp->next;
		}
		echo " → NULL\n";
	}
	// This is handle the request of folding given linked list nodes
	public	function foldList()
	{
		if ($this->head == null)
		{
			echo "\n Empty linked list\n";
			return;
		}
		$temp = $this->head;
		$auxiliary = $this->head;
		$hold = null;
		// Loop which Find middle node
		while ($temp != null && $temp->next != null && $temp->next->next != null)
		{
			$auxiliary = $auxiliary->next;
			$temp = $temp->next->next;
		}
		if ($auxiliary != null)
		{
			$hold = $auxiliary;
			$auxiliary = $auxiliary->next;
			// Separating half nodes
			$hold->next = null;
			// Start with first node of next half section
			$temp = $auxiliary;
			$auxiliary = null;
			// Reverse second half nodes
			while ($temp != null)
			{
				$hold = $temp;
				$temp = $temp->next;
				$hold->next = $auxiliary;
				$auxiliary = $hold;
			}
			// Start with first node of linked list
			$temp = $this->head;
			// Combine first and second half nodes in alternate manner
			while ($temp != null)
			{
				$hold = $temp;
				$temp = $temp->next;
				$hold->next = $auxiliary;
				if ($auxiliary != null)
				{
					$hold = $auxiliary;
					$auxiliary = $auxiliary->next;
					$hold->next = $temp;
				}
			}
		}
	}
}

function main()
{
	// Create a empty linked lists
	$sll1 = new SingleLL();
	$sll2 = new SingleLL();
	//  Constructed 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);
	echo " Before folding \n";
	$sll1->display();
	// Form of folding
	//      1 → 2 → 3 → 4 ──┐
	// NULL ← 8 ← 7 ← 6 ← 5⤶
	$sll1->foldList();
	// After merging
	// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	echo " After folding \n";
	$sll1->display();
	// Constructed second linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	$sll2->addNode(1);
	$sll2->addNode(2);
	$sll2->addNode(3);
	$sll2->addNode(4);
	$sll2->addNode(5);
	$sll2->addNode(6);
	$sll2->addNode(7);
	echo " Before folding \n";
	$sll2->display();
	// Form of folding
	//      1 → 2 → 3 → 4
	// NULL ← 7 ← 6 ← 5⤶
	$sll2->foldList();
	// After merging
	// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	echo " After folding \n";
	$sll2->display();
}
main();

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
/*
  Node Js Program
  Fold Linked List
*/
// 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(" →");
			}
			process.stdout.write(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		process.stdout.write(" → NULL\n");
	}
	// This is handle the request of folding given linked list nodes
	foldList()
	{
		if (this.head == null)
		{
			process.stdout.write("\n Empty linked list\n");
			return;
		}
		var temp = this.head;
		var auxiliary = this.head;
		var hold = null;
		// Loop which Find middle node
		while (temp != null && temp.next != null && temp.next.next != null)
		{
			auxiliary = auxiliary.next;
			temp = temp.next.next;
		}
		if (auxiliary != null)
		{
			hold = auxiliary;
			auxiliary = auxiliary.next;
			// Separating half nodes
			hold.next = null;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = null;
			// Reverse second half nodes
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this.head;
			// Combine first and second half nodes in alternate manner
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				if (auxiliary != null)
				{
					hold = auxiliary;
					auxiliary = auxiliary.next;
					hold.next = temp;
				}
			}
		}
	}
}

function main()
{
	// Create a empty linked lists
	var sll1 = new SingleLL();
	var sll2 = new SingleLL();
	//  Constructed 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);
	process.stdout.write(" Before folding \n");
	sll1.display();
	// Form of folding
	//      1 → 2 → 3 → 4 ──┐
	// NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList();
	// After merging
	// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	process.stdout.write(" After folding \n");
	sll1.display();
	// Constructed second linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1);
	sll2.addNode(2);
	sll2.addNode(3);
	sll2.addNode(4);
	sll2.addNode(5);
	sll2.addNode(6);
	sll2.addNode(7);
	process.stdout.write(" Before folding \n");
	sll2.display();
	// Form of folding
	//      1 → 2 → 3 → 4
	// NULL ← 7 ← 6 ← 5⤶
	sll2.foldList();
	// After merging
	// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	process.stdout.write(" After folding \n");
	sll2.display();
}
main();

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
#   Python 3 Program
#   Fold 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
	
	# 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 = "")
			
			print("", temp.data, end = "")
			#  Visit to next node
			temp = temp.next
		
		print(" → NULL")
	
	#  This is handle the request of folding given linked list nodes
	def foldList(self) :
		if (self.head == None) :
			print("\n Empty linked list")
			return
		
		temp = self.head
		auxiliary = self.head
		hold = None
		#  Loop which Find middle node
		while (temp != None and temp.next != None and temp.next.next != None) :
			auxiliary = auxiliary.next
			temp = temp.next.next
		
		if (auxiliary != None) :
			hold = auxiliary
			auxiliary = auxiliary.next
			#  Separating half nodes
			hold.next = None
			#  Start with first node of next half section
			temp = auxiliary
			auxiliary = None
			#  Reverse second half nodes 
			while (temp != None) :
				hold = temp
				temp = temp.next
				hold.next = auxiliary
				auxiliary = hold
			
			#  Start with first node of linked list
			temp = self.head
			#  Combine first and second half nodes in alternate manner
			while (temp != None) :
				hold = temp
				temp = temp.next
				hold.next = auxiliary
				if (auxiliary != None) :
					hold = auxiliary
					auxiliary = auxiliary.next
					hold.next = temp
				
			
		
	

def main() :
	#  Create a empty linked lists
	sll1 = SingleLL()
	sll2 = SingleLL()
	#   Constructed 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)
	print(" Before folding ")
	sll1.display()
	#  Form of folding
	#       1 → 2 → 3 → 4 ──┐
	#  NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList()
	#  After merging
	#  1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	print(" After folding ")
	sll1.display()
	#  Constructed second linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1)
	sll2.addNode(2)
	sll2.addNode(3)
	sll2.addNode(4)
	sll2.addNode(5)
	sll2.addNode(6)
	sll2.addNode(7)
	print(" Before folding ")
	sll2.display()
	#  Form of folding
	#       1 → 2 → 3 → 4 
	#  NULL ← 7 ← 6 ← 5⤶
	sll2.foldList()
	#  After merging
	#  1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	print(" After folding ")
	sll2.display()

if __name__ == "__main__": main()

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
#   Ruby Program
#   Fold 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
	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

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

		print(" → NULL\n")
	end

	#  This is handle the request of folding given linked list nodes
	def foldList() 
		if (self.head == nil) 
			print("\n Empty linked list\n")
			return
		end

		temp = self.head
		auxiliary = self.head
		hold = nil
		#  Loop which Find middle node
		while (temp != nil && temp.next != nil && temp.next.next != nil) 
			auxiliary = auxiliary.next
			temp = temp.next.next
		end

		if (auxiliary != nil) 
			hold = auxiliary
			auxiliary = auxiliary.next
			#  Separating half nodes
			hold.next = nil
			#  Start with first node of next half section
			temp = auxiliary
			auxiliary = nil
			#  Reverse second half nodes 
			while (temp != nil) 
				hold = temp
				temp = temp.next
				hold.next = auxiliary
				auxiliary = hold
			end

			#  Start with first node of linked list
			temp = self.head
			#  Combine first and second half nodes in alternate manner
			while (temp != nil) 
				hold = temp
				temp = temp.next
				hold.next = auxiliary
				if (auxiliary != nil) 
					hold = auxiliary
					auxiliary = auxiliary.next
					hold.next = temp
				end

			end

		end

	end

end

def main() 
	#  Create a empty linked lists
	sll1 = SingleLL.new()
	sll2 = SingleLL.new()
	#   Constructed 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)
	print(" Before folding \n")
	sll1.display()
	#  Form of folding
	#       1 → 2 → 3 → 4 ──┐
	#  NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList()
	#  After merging
	#  1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	print(" After folding \n")
	sll1.display()
	#  Constructed second linked list
	#   1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1)
	sll2.addNode(2)
	sll2.addNode(3)
	sll2.addNode(4)
	sll2.addNode(5)
	sll2.addNode(6)
	sll2.addNode(7)
	print(" Before folding \n")
	sll2.display()
	#  Form of folding
	#       1 → 2 → 3 → 4 
	#  NULL ← 7 ← 6 ← 5⤶
	sll2.foldList()
	#  After merging
	#  1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	print(" After folding \n")
	sll2.display()
end

main()

Output

 Before folding 
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding 
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding 
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding 
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
/*
  Scala Program
  Fold 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)
{
	def this()
	{
		this(null);
	}
	//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;
		}
		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(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// This is handle the request of folding given linked list nodes
	def foldList(): Unit = {
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode = this.head;
		var auxiliary: LinkNode = this.head;
		var hold: LinkNode = null;
		// Loop which Find middle node
		while (temp != null && temp.next != null && temp.next.next != null)
		{
			auxiliary = auxiliary.next;
			temp = temp.next.next;
		}
		if (auxiliary != null)
		{
			hold = auxiliary;
			auxiliary = auxiliary.next;
			// Separating half nodes
			hold.next = null;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = null;
			// Reverse second half nodes
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this.head;
			// Combine first and second half nodes in alternate manner
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				if (auxiliary != null)
				{
					hold = auxiliary;
					auxiliary = auxiliary.next;
					hold.next = temp;
				}
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		// Create a empty linked lists
		var sll1: SingleLL = new SingleLL();
		var sll2: SingleLL = new SingleLL();
		//  Constructed 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);
		print(" Before folding \n");
		sll1.display();
		// Form of folding
		//      1 → 2 → 3 → 4 ──┐
		// NULL ← 8 ← 7 ← 6 ← 5⤶
		sll1.foldList();
		// After merging
		// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
		print(" After folding \n");
		sll1.display();
		// Constructed second linked list
		//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
		sll2.addNode(1);
		sll2.addNode(2);
		sll2.addNode(3);
		sll2.addNode(4);
		sll2.addNode(5);
		sll2.addNode(6);
		sll2.addNode(7);
		print(" Before folding \n");
		sll2.display();
		// Form of folding
		//      1 → 2 → 3 → 4
		// NULL ← 7 ← 6 ← 5⤶
		sll2.foldList();
		// After merging
		// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
		print(" After folding \n");
		sll2.display();
	}
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
/*
  Swift 4 Program
  Fold 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? ;
	init()
	{
		self.head = 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
		{
			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: "");
			}
			print(" ", temp!.data, terminator: "");
			// Visit to next node
			temp = temp!.next;
		}
		print(" → NULL");
	}
	// This is handle the request of folding given linked list nodes
	func foldList()
	{
		if (self.head == nil)
		{
			print("\n Empty linked list");
			return;
		}
		var temp: LinkNode? = self.head;
		var auxiliary: LinkNode? = self.head;
		var hold: LinkNode? = nil;
		// Loop which Find middle node
		while (temp  != nil && temp!.next  != nil && temp!.next!.next  != nil)
		{
			auxiliary = auxiliary!.next;
			temp = temp!.next!.next;
		}
		if (auxiliary  != nil)
		{
			hold = auxiliary;
			auxiliary = auxiliary!.next;
			// Separating half nodes
			hold!.next = nil;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = nil;
			// Reverse second half nodes
			while (temp  != nil)
			{
				hold = temp;
				temp = temp!.next;
				hold!.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = self.head;
			// Combine first and second half nodes in alternate manner
			while (temp  != nil)
			{
				hold = temp;
				temp = temp!.next;
				hold!.next = auxiliary;
				if (auxiliary  != nil)
				{
					hold = auxiliary;
					auxiliary = auxiliary!.next;
					hold!.next = temp;
				}
			}
		}
	}
}
func main()
{
	// Create a empty linked lists
	let sll1: SingleLL = SingleLL();
	let sll2: SingleLL = SingleLL();
	//  Constructed 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);
	print(" Before folding ");
	sll1.display();
	// Form of folding
	//      1 → 2 → 3 → 4 ──┐
	// NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList();
	// After merging
	// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	print(" After folding ");
	sll1.display();
	// Constructed second linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1);
	sll2.addNode(2);
	sll2.addNode(3);
	sll2.addNode(4);
	sll2.addNode(5);
	sll2.addNode(6);
	sll2.addNode(7);
	print(" Before folding ");
	sll2.display();
	// Form of folding
	//      1 → 2 → 3 → 4
	// NULL ← 7 ← 6 ← 5⤶
	sll2.foldList();
	// After merging
	// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	print(" After folding ");
	sll2.display();
}
main();

Output

 Before folding
  1 →  2 →  3 →  4 →  5 →  6 →  7 →  8 → NULL
 After folding
  1 →  8 →  2 →  7 →  3 →  6 →  4 →  5 → NULL
 Before folding
  1 →  2 →  3 →  4 →  5 →  6 →  7 → NULL
 After folding
  1 →  7 →  2 →  6 →  3 →  5 →  4 → NULL
/*
  Kotlin Program
  Fold 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 ? ;
	constructor()
	{
		this.head = null;
	}
	//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;
		}
		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(" →");
			}
			print(" " + temp.data);
			// Visit to next node
			temp = temp.next;
		}
		print(" → NULL\n");
	}
	// This is handle the request of folding given linked list nodes
	fun foldList(): Unit
	{
		if (this.head == null)
		{
			print("\n Empty linked list\n");
			return;
		}
		var temp: LinkNode ? = this.head;
		var auxiliary: LinkNode ? = this.head;
		var hold: LinkNode? ;
		// Loop which Find middle node
		while (temp != null && temp.next != null && temp.next?.next != null)
		{
			auxiliary = auxiliary?.next;
			temp = temp.next?.next;
		}
		if (auxiliary != null)
		{
			hold = auxiliary;
			auxiliary = auxiliary.next;
			// Separating half nodes
			hold.next = null;
			// Start with first node of next half section
			temp = auxiliary;
			auxiliary = null;
			// Reverse second half nodes
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				auxiliary = hold;
			}
			// Start with first node of linked list
			temp = this.head;
			// Combine first and second half nodes in alternate manner
			while (temp != null)
			{
				hold = temp;
				temp = temp.next;
				hold.next = auxiliary;
				if (auxiliary != null)
				{
					hold = auxiliary;
					auxiliary = auxiliary.next;
					hold.next = temp;
				}
			}
		}
	}
}
fun main(args: Array < String > ): Unit
{
	// Create a empty linked lists
	var sll1: SingleLL = SingleLL();
	var sll2: SingleLL = SingleLL();
	//  Constructed 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);
	print(" Before folding \n");
	sll1.display();
	// Form of folding
	//      1 → 2 → 3 → 4 ──┐
	// NULL ← 8 ← 7 ← 6 ← 5⤶
	sll1.foldList();
	// After merging
	// 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
	print(" After folding \n");
	sll1.display();
	// Constructed second linked list
	//  1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
	sll2.addNode(1);
	sll2.addNode(2);
	sll2.addNode(3);
	sll2.addNode(4);
	sll2.addNode(5);
	sll2.addNode(6);
	sll2.addNode(7);
	print(" Before folding \n");
	sll2.display();
	// Form of folding
	//      1 → 2 → 3 → 4
	// NULL ← 7 ← 6 ← 5⤶
	sll2.foldList();
	// After merging
	// 1 → 7 → 2 → 6 → 3 → 5 → 4 → NULL
	print(" After folding \n");
	sll2.display();
}

Output

 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
 After folding
 1 → 8 → 2 → 7 → 3 → 6 → 4 → 5 → NULL
 Before folding
 1 → 2 → 3 → 4 → 5 → 6 → 7 → NULL
 After folding
 1 → 7 → 2 → 6 → 3 → 5 → 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