Skip to content

Commit

Permalink
Update ReverseLL.java
Browse files Browse the repository at this point in the history
  • Loading branch information
harshithasudhakar authored Feb 7, 2025
1 parent d69eba3 commit edc3b98
Showing 1 changed file with 56 additions and 17 deletions.
73 changes: 56 additions & 17 deletions Linked List/Singly Linked List/ReverseLL.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,69 @@
/*
public class Main {
import java.util.*;

static class Node {
int data;
Node next;
class ReversingSLL{

public Node(int data) {
this.data = data;
static class Node{
int val;
Node next;
Node(int val){
this.val = val;
this.next = null;
}
}*/
public static Node reverseList(Node head) {
Node prev = null;
}

static Node head;

static void addNode(int val){
Node current = head;
Node next = null;
Node newNode = new Node(val);
if(head == null){
head = newNode;
return;
}
while(current.next != null){
current = current.next;
}
current.next = newNode;
}

while (current != null) {
next = current.next;
static Node reverse(Node head){
Node prev = null;
Node current = head;

while(current!=null){

Node nextNode = current.next;
current.next = prev;

prev = current;
current = next;
current = nextNode;
}

return prev;
}


static void printList(Node head){
Node current = head;
while(current!=null){
System.out.print(current.val + "->");
current = current.next;
}
System.out.println("null");
}

public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the no.of nodes: ");
int n = input.nextInt();
int[] nodes = new int[n];
System.out.println("Enter the nodes: ");
for(int i = 0; i<n; i++){
nodes[i] = input.nextInt();
}

for(int i = 0; i<n; i++){
addNode(nodes[i]);
}

head = reverse(head);
printList(head);
}
}

0 comments on commit edc3b98

Please sign in to comment.