Write a Java program to Reverse alternate K nodes in a Singly Linked List


The code provided is an implementation of a Reverse_AlternateKNodes class in Java that reverses alternate k nodes in a linked list. The linked list is represented using a Node class with an integer data field and a next pointer. The Reverse_AlternateKNodes class has the following methods:

  • kAltReverse(Node node, int k) : This method reverses alternate k nodes in the linked list. It takes two pointers, current and prev, to keep track of the current and previous nodes respectively. It also keeps track of the next node in the original list using the next pointer. It reverses k nodes by updating the next pointer of each node to point to its previous node. After reversing k nodes, it connects the last node of the reversed sublist to the next node in the original list. Then, it moves the current pointer to the next k nodes and recursively calls the kAltReverse method on the remaining nodes. Finally, it returns the head of the reversed sublist.
  • printList(Node node) : This method prints the values of the nodes in the linked list, separated by a space. It takes a node as an input parameter and iterates through the linked list using the next pointers, printing the values of the nodes.
  • push(int newdata) : This method pushes a new node with the given value to the front of the linked list. It creates a new node with the given value, sets its next pointer to the current head, and updates the head to be the new node.
  • main(String[] args) : This is the main method that creates an instance of the Reverse_AlternateKNodes class, pushes values from 20 to 1 to the linked list, prints the original linked list, calls the kAltReverse method with k=6, and prints the modified linked list.

Note: The implementation assumes 0-based indexing for counting the nodes in the linked list, meaning the first node is index 0. If you want to use 1-based indexing, you need to adjust the implementation accordingly. Also, the implementation does not handle cases where k is larger than the size of the linked list or when there are less than k nodes remaining to reverse, so make sure to handle such cases if needed.

Source Code

class Reverse_AlternateKNodes
{
 
	public static void main(String[] args)
	{
		Reverse_AlternateKNodes list = new Reverse_AlternateKNodes();
 
		for (int i = 20; i > 0; i--)
		{
			list.push(i);
		}
		System.out.print("Given Linked List : ");
		list.printList(head);
		head = list.kAltReverse(head, 6);
		System.out.println("");
		System.out.print("Modified Linked List : ");
		list.printList(head);
 
	}
 
	static Node head;
 
	class Node
	{
 
		int data;
		Node next;
 
		Node(int d)
		{
			data = d;
			next = null;
		}
	}
 
	Node kAltReverse(Node node, int k)
	{
		Node current = node;
		Node next = null, prev = null;
		int count = 0;
 
		while (current != null && count < k)
		{
			next = current.next;
			current.next = prev;
			prev = current;
			current = next;
			count++;
		}
 
		if (node != null)
		{
			node.next = current;
		}
 
		count = 0;
		while (count < k - 1 && current != null)
		{
			current = current.next;
			count++;
		}
 
		if (current != null)
		{
			current.next = kAltReverse(current.next, k);
		}
		return prev;
	}
 
	void printList(Node node)
	{
		while (node != null)
		{
			System.out.print(node.data + " ");
			node = node.next;
		}
	}
 
	void push(int newdata)
	{
		Node mynode = new Node(newdata);
		mynode.next = head;
		head = mynode;
	}
 
}

Output

Given Linked List : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Modified Linked List : 6 5 4 3 2 1 7 8 9 10 11 12 18 17 16 15 14 13 19 20

Example Programs