forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseList.java
74 lines (74 loc) · 2.35 KB
/
ReverseList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// In this code a linkedlist is taken as input and the reversed form is displayed
import java.util.*;
public class ReverseList {
public static void main(String args[])
{
try (Scanner sc = new Scanner(System.in)) {
ListNode fresh,start=null,prev=null,ptr=null;
int c;
do
{
fresh=new ListNode();
System.out.println("Enter the data");
fresh.data=sc.nextInt();
if(start==null)
{
start=fresh;
// stores the starting address
}
else
{
prev.next=fresh;
}
prev=fresh;
System.out.println("Press 1 to for next node initialization else press any number other than 1 to exit");
c=sc.nextInt();
// for next node initialization option is asked from the user
}while(c==1);
Solution nm=new Solution();
System.out.println("The original LinkedList is ");
for(ptr=start;ptr!=null;ptr=ptr.next)
{
System.out.print(ptr.data+" ");
}
System.out.println();
// displayes the original linkedlist
System.out.println("The reversed LinkedList is ");
nm.reverseList(start);
for(ptr=start;ptr!=null;ptr=ptr.next)
{
System.out.print(ptr.data+" ");
}
System.out.println();
// displays the reserved linkedlist
}
}
}
class Solution
{
public ListNode reverseList(ListNode start)
{
if(start==null)
return null;
// if there is no elements in the linkedlist then it returns null
else
{
ListNode ptr,ptr1,ptr2;
for(ptr=null,ptr1=start,ptr2=start.next,ptr1.next=null;ptr2!=null;ptr2=ptr2.next,ptr1.next=ptr)
{
ptr=ptr1;
ptr1=ptr2;
}
start=ptr1;
return start;
// returns the start position of the linkedlist after reversing the list
}
}
}
// Solution class contains reverseList method which takes the start address as input and reverses the whole linkedlist
class ListNode
{
int data;
ListNode next;
}
// ListNode class is created for linklist creation