Java Program to fold a Linked List

Java Program for folding a given Linked List

Java Program for folding a given Linked List . It is a user defined or user input program. To fold a linked list we have to first create a simple linked list. When we fold a linked list it will take place between the nodes.  We can say that at every step we change the next of the left node such that it points to the right node. the next of a right node also change accordingly.

Java Program for folding a given Linked List .

Java program to fold a linked list

When we fold a linked list it will take place between the nodes.  We can say that at every step we change the next of the left node such that it points to the right node. the next of a right node also change accordingly.

Example:

Given Linked list : 1–>2–>3–>4–>5–>6

Output of the given linked list after folding

1–>6–>2–>5–>3–>4

Fold a Linked List in Java

Code in JAVA Programming Language 

Run
import java.lang.*;

class LinkedList
{
  Node head;

  // Node Class
  class Node
  {
    int data;
    Node next;

      Node (int x)		// parameterized constructor
    {
      data = x;
      next = null;
    }
  }

  public void foldLinkedList (Node head)
  {
    Node slowPointer = head;
    Node fastPointer = head;
    while (fastPointer != null)
      {
	slowPointer = slowPointer.next;
	fastPointer = fastPointer.next;
	if (fastPointer != null)
	  fastPointer = fastPointer.next;
      }
    Node middlePointer = slowPointer;
    Node reverseLastHalf = reverseLinkedList (slowPointer);
    while (reverseLastHalf != null && head != middlePointer)
      {
	Node tempHead = head.next;
	Node tempReverse = reverseLastHalf.next;
	reverseLastHalf.next = head.next;
	head.next = reverseLastHalf;
	head = tempHead;
	reverseLastHalf = tempReverse;
      }
    if (reverseLastHalf != null)
      reverseLastHalf.next = null;
    else
      head.next = null;
  }

  public static Node reverseLinkedList (Node head)
  {
    if (head.next == null)
      return head;
    Node newHead = reverseLinkedList (head.next);
    head.next.next = head;
    head.next = null;
    return newHead;
  }


  public Node insert (int data)
  {
    Node newNode = new Node (data);
    newNode.next = head;
    head = newNode;

    return head;
  }

  public void display ()
  {
    Node node = head;
    //as linked list will end when Node reaches Null
    while (node != null)
      {
	System.out.print (node.data + " ");
	node = node.next;
      }
    System.out.println ("\n");
  }
}

public class Main
{
  public static void main (String args[])
  {
    LinkedList ll = new LinkedList ();

      ll.insert (6);
      ll.insert (5);
      ll.insert (4);
      ll.insert (3);
      ll.insert (2);
      ll.insert (1);
      
      System.out.println("Linked List before fold");
      ll.display();

      ll.foldLinkedList (ll.head);
      
      System.out.println("Linked List after fold");
      ll.display ();


  }
}
Output: 
Linked List before fold
1 2 3 4 5 6 

Linked List after fold
1 6 2 5 3 4 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java