-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseLL.java
More file actions
67 lines (67 loc) · 1.23 KB
/
reverseLL.java
File metadata and controls
67 lines (67 loc) · 1.23 KB
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
import java.util.*;
public class Main
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
}
}
static Node head;
void insert(int data)
{
Node n=new Node(data);
if(head==null)
{
head=n;
return;
}
Node ptr=head;
while(ptr.next!=null)
{
ptr=ptr.next;
}
ptr.next=n;
return;
}
void print(Node head)
{
Node ptr=head;
while(ptr!=null)
{
System.out.print(ptr.data+" ");
ptr=ptr.next;
}
}
Node reverse(Node head)
{
Node next=null;
Node current=head;
Node prev=null;
while(current!=null)
{
next=current.next;
current.next=prev;
prev=current;
current=next;
}
head=prev;
return head;
}
public static void main(String[] args) {
Main a= new Main();
Scanner s=new Scanner(System.in);
System.out.println("Enter the size");
int size=s.nextInt();
while(size--!=0)
{
a.insert(s.nextInt());
}
head=a.reverse(head);
System.out.println("after reversing");
a.print(head);
}
}