-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d69eba3
commit edc3b98
Showing
1 changed file
with
56 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |