C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs

Java program to create and reverse a linked list

In this article of java programs, we will learn how to create and reverse a linked list?
Submitted by Jyoti Singh, on December 04, 2017

A linked list contains two things data and the address of the node each node is linked to the next node. Here, we are implementing a program that will create and reverse a linked list.

create and reverse a linked list

First we will create a class named node to create a node with data and node (next node) as it’s field.ReverseList() method will reverse the list by changing the links of the nodes or by reversing the links.Printlist() will print the reversed liked list.

Program to reverse a linked list in java

public class LinkedList{
	//head object of class node will point to the 
	//head of the linked list
	Node head;
	//class node to create a node with data and 
	//next node (pointing to node)
	public static class Node{
		int data;
		Node next;
		Node(int data,Node next){
			this.data=data;
			this.next=next;
		}
	}

	public static void main (String[] args){
		//linked list object ll
		LinkedList ll=new LinkedList();
		Node first=new Node(1,null);
		Node second=new Node(2,null);
		Node third=new Node(3,null);
		/*three node will be created dynamically
		 
		   first           second              third
			 |                |                  |
			 |                |                  |
		 +----+------+     +----+------+     +----+------+
		 | 1  | null |     | 2  | null |     |  3 | null |
		 +----+------+     +----+------+     +----+------+ */

		ll.head=first;
		/* now first node will be head of the linkedlist ll
		 
		ll.head          second              third
		  |                |                  |
		  |                |                  |
		+----+------+     +----+------+     +----+------+
		| 1  | null |     | 2  | null |     |  3 | null |
		+----+------+     +----+------+     +----+------+ */

		first.next=second;
		/* now next of first node will point to second
		 
		ll.head          second              third
		  |                |                  |
		  |                |                  |
		+----+------+     +----+------+     +----+------+
		| 1  |  o-------->| 2  | null |     |  3 | null |
		+----+------+     +----+------+     +----+------+ */
		second.next=third;
		/* here next of third node will point to third
		 
		ll.head          second              third
		  |                |                  |
		  |                |                  |
		+----+------+     +----+------+     +----+------+
		| 1  |  o-------->| 2  |  o-------> |  3 | null |
		+----+------+     +----+------+     +----+------+ */
		ll.printlist();
		ll.reverselist();
		ll.printlist();
	}

	//this function will print the list
	public void printlist() {
		Node n= head;
		//loop continues until it points to null
		while(n!=null){
			System.out.print(n.data);
			n=n.next;
		}
		System.out.println();
	}

	//this function will reverse the list 
	//by changing current node to previous
	public void reverselist() {
		Node current=head;
		Node prev=null;
		Node nextnode=null;
		//for first node previous node will be null
		while(current!=null){
			//reverse the link by assigning current.nextnode 
			//to previous node and move current node and 
			//previous node by 1
			nextnode=current.next;
			current.next=prev;
			prev=current;
			current=nextnode;	
		}
		//head of the list is assigned as prevous node 
		//which will contain the last node 
		head=prev;
	}
}

Output

123
321








COMMENTS