Skip to content

Commit 07e7047

Browse files
Merge pull request PrajaktaSathe#135 from siddhant2002/main
Code added for Reverse a LinkedList
2 parents 1566503 + faad14f commit 07e7047

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

Programs/ReverseList.java

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ It is very easy to contribute, you may follow these steps -
109109
82. [SwitchingLetters](https://github.com/PrajaktaSathe/Java/tree/main/Programs/SwitchingLetters.java) - Program that demonstrates switching letters according to given indexes.
110110
83. [Matrix Multiplication](https://github.com/siddhant2002/Java-1/blob/main/Programs/matrix/Matrix_Multiply.java) - Program to multiply two matrices.
111111
84. [Sieve of Eratosthenes](https://github.com/siddhant2002/Java-1/blob/main/Programs/Sieve_Of_Eratosthenes.java) - Program to multiply two matrices.
112-
112+
85. [Reverse a LinkedList](https://github.com/siddhant2002/Java-1/blob/main/Programs/ReverseList.java) - Program to reverse a linkedList.
113113
# Contributors -
114114
## A big thank you to all our contributors!!!
115115

0 commit comments

Comments
 (0)