• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Reverse a Linked List In JAVA [Complete Code]

Sachin Sharma/1641/0
CodeJAVA

Problem statement: You have given a pointer to the head node of a linked list, the task is to reverse the linked list.

Example: 1->2->3->4->5->null
Output: 5->4->3->2->1->null
Reverse a Linked List in Java

Prerequisite:

  1. Basics of Java programming
  2. Understanding of the Linked List.
  3. Basic understanding of classes and objects in Java.
  4. Swapping

In this tutorial, we are writing Java program to reverse linked list. You can also check C++ code to reverse linked list.

[Solution] Reverse a Linked List In JAVA

import java.util.*;

class Main {
	public static class ListNode {
    	int val = 0;
    	ListNode next = null;

    	ListNode(int val) {
      		this.val = val;
    	}
	}

	// we will get each node data and from beginning to last 
	// and swap them with each other
	public static ListNode reverse(ListNode head) {
		ListNode n = head;
    	int size = 0;
    	while (n != null) {
        	size ++;
        	n = n.next;
    	}
    	int i = 0, j = size - 1;
    	while (i < j) {
        	ListNode a = getNode(i, head);
        	ListNode b = getNode(j, head);
        	int temp = a.val;
        	a.val = b.val;
        	b.val = temp;
        	i++;
        	j--;
    	}
    	return head;;
	}

	private static ListNode getNode(int indx, ListNode head) {
   		ListNode n = head;
   		if (indx == 0)
     		return head;
    	for (int i = 0; i < indx; i++) {
        	n = n.next;
    	}
    	return n;
	}

	public static void main(String[] args) {
    	Scanner scn = new Scanner(System.in);
    	int n = scn.nextInt();
    	ListNode dummy = new ListNode(-1);
    	ListNode prev = dummy;
    	while (n-- > 0) {
        	prev.next = new ListNode(scn.nextInt());
        	prev = prev.next;
    	}

    	ListNode head = reverse(dummy.next);
    	while (head != null) {
        	System.out.print(head.val + " ");
			head = head.next;
    	}
	}
}

As we are using Java basics syntax to implement this logic, it will work for any Java versions like Java 8, Java 12,…

You can implement this logic in C/C++ and python as well.

This is all about this tutorial to reverse a Linked List In JAVA. If you have any questions or point to discuss, let me know in the comment.

JavaLinked List
Sachin Sharma
I am Sachin Sharma a complete JAVA geek and I also have good knowledge of Python, C, C++, and front-end development. I am a 5-star gold coder on Hckerrank in JAVA, Python, C++, and DSA. I am eager to share my coding knowledge to help people.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Basic Java Tutorial

  1. Java- Tutorial Overview
  2. Java- Features & Characteristics
  3. Java-  Installation & Setup
  4. Java- Hello, World Program!
  5. Java- JDK vs JVM vs JRE
  6. Java- Data Types & Variables
  7. Java- String & its Methods
  8. Java- Different Operators Types
  9. Java- Flow Control Statements
  10. Java- Array
  11. Java- Exception Handling
  12. Java- ‘throw’ vs ‘throws’ Keyword
  13. Java- RegEx
  14. Java 12- New Advanced Features

Java OOPs concepts

  1. Java- OOPs Introduction
  2. Java- Classes & Objects
  3. Java- Constructor & Overloading
  4. Java- Method Overload vs Override
  5. Java- Access Modifiers
  6. Java- Abstraction
  7. Java- Inheritance
  8. Java- Interfaces
  9. Java- Nested Inner Classes

Java Advanced

  1. Java- Applet vs Application
  2. Java- HashMap Collections
  3. Java- ArrayList
  4. Java- LinkedList
  5. Java- HashSet
  6. Java- HashMap vs HashSet
  7. Java- Reverse Linked List

Java Exercise

50+ Java Coding Questions [Practice]

Java Projects

Patient Billing Software

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard