To reverse a linked list we need to change the pointers so in the reverseList method i have taken 3 pointers next, prev, current,
current pointer is head at starting and next and prev are null.
in while loop we will check current pointer if it is null or now if its null it means it reached to last of the list and that time while loop will break.
inside while loop first we are assigning next = current.next here next will hold the next pointer of current and current.next = prev it will change the pointer and make the previous node as next node for the current, then prev = current assign current node to prev node and current = next, this will be keep running till current =null and then loop will break, at last our head = prev , prev node will be assigned to head because at this time last node is prev and current node is null.
public class ReverseLinkedList {
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
void addData(int d) {
Node newNode = new Node(d);
if(head == null)head = newNode;
else {
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
void printList() {
Node temp = head;
while(temp !=null) {
System.out.print(temp.data+" ");
temp = temp.next;
}
}
void reverseL() {
reverseList(head);
}
void reverseList(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static void main(String[] args) {
ReverseLinkedList l = new ReverseLinkedList();
for(int i=1;i<=5;i++) {
l.addData(i);
}
l.printList();
System.out.println();
l.reverseL();
l.printList();
}
}
current pointer is head at starting and next and prev are null.
in while loop we will check current pointer if it is null or now if its null it means it reached to last of the list and that time while loop will break.
inside while loop first we are assigning next = current.next here next will hold the next pointer of current and current.next = prev it will change the pointer and make the previous node as next node for the current, then prev = current assign current node to prev node and current = next, this will be keep running till current =null and then loop will break, at last our head = prev , prev node will be assigned to head because at this time last node is prev and current node is null.
public class ReverseLinkedList {
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data = data;
next = null;
}
}
void addData(int d) {
Node newNode = new Node(d);
if(head == null)head = newNode;
else {
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
void printList() {
Node temp = head;
while(temp !=null) {
System.out.print(temp.data+" ");
temp = temp.next;
}
}
void reverseL() {
reverseList(head);
}
void reverseList(Node node) {
Node prev = null;
Node current = node;
Node next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static void main(String[] args) {
ReverseLinkedList l = new ReverseLinkedList();
for(int i=1;i<=5;i++) {
l.addData(i);
}
l.printList();
System.out.println();
l.reverseL();
l.printList();
}
}
Tags:
Simple Programs