-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListSwapPairs.java
More file actions
56 lines (54 loc) · 1.3 KB
/
LinkedListSwapPairs.java
File metadata and controls
56 lines (54 loc) · 1.3 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
package Codes;
import java.util.Scanner;
public class LinkedListSwapPairs {
static Node head;
static class Node{
int data;
Node next;
Node(int d){
data=d;
next=null;
}
}
static void append(int d){
Node newnode=new Node(d);
if (head==null)
head=newnode;
else {
Node temp=head;
while (temp.next!=null){
temp=temp.next;
}
temp.next=newnode;
}
}
static void display(){
Node temp=head;
while (temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
}
static void swap(){
Node one=head;
while (one!=null && one.next!=null){
int temp=one.data;
one.data=one.next.data;
one.next.data=temp;
one=one.next.next;
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String input=sc.nextLine();
String[] arr=input.split(" ");
for (int i=0;i<arr.length;i++){
append(Integer.parseInt(arr[i]));
}
display();
System.out.println("NULL");
swap();
display();
System.out.println("NULL");
}
}