forked from joharbatta/DataStructure-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswapll.java
More file actions
63 lines (63 loc) · 900 Bytes
/
Copy pathswapll.java
File metadata and controls
63 lines (63 loc) · 900 Bytes
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
import java.util.*;
public class swapll
{
static class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
next=null;
}
}
static Node head;
void insert(int data)
{
Node tem=new Node(data);
if(head==null)
{
head=tem;
return;
}
Node cur=head;
while(cur.next!=null)
{
cur=cur.next;
}
cur.next=tem;
}
void swap()
{
Node cur=head;
while(cur!=null&&cur.next!=null)
{
int a=cur.data;
cur.data=cur.next.data;
cur.next.data=a;
cur=cur.next.next;
}
}
void print()
{
Node cur=head;
while(cur!=null)
{
System.out.print(cur.data+" ");
cur=cur.next;
}
}
public static void main(String args[])
{
swapll s=new swapll();
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size");
int size=sc.nextInt();
for(int i=0;i<size;i++)
{
s.insert(sc.nextInt());
}
s.swap();
s.print();
}
}