-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.java
105 lines (93 loc) · 2.37 KB
/
LinkList.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* LinkList
*/
public class LinkList {
// creating a Node class
public class Node{
int data;
Node next=null;
Node(int data){
this.data=data;
this.next=null;
}
}
// creating head and tail static for once, so no need to create it again and again
public static Node head;
public static Node tail;
public static int size=0;
// function to add node in LL at fist position
public void addFirst(int data){
Node newNode = new Node(data);
if (head==null) {
tail=newNode;
}
newNode.next=head;
head=newNode;
size+=1;
}
// function to add node in LL at last position
public void addLast(int data){
Node newNode = new Node(data);
if (head==null) {
head=newNode;
}
tail.next=newNode;
tail=newNode;
size+=1;
}
//function to print the data in linklist
public void printLL(){
Node temp = head;
while (temp!=null) {
System.out.print(temp.data + " ");
temp=temp.next;
}
System.out.println();
}
//Function to add node at certain position in lidnk list
public void addAt(int pos,int data){
Node newNode = new Node(data);
Node temp=head;
for (int i = 0; i < pos-1; i++) {
if (temp.next==null) {
System.out.println("Index not found !!!");
return;
}
temp=temp.next;
}
newNode.next=temp.next;
temp.next=newNode;
size+=1;
}
//reverses linklist
public void reverse(){
Node prevNode = null;
Node currNode = head;
Node nextNode;
tail= head;
while(currNode!=null){
nextNode=currNode.next;
currNode.next=prevNode;
prevNode=currNode;
currNode = nextNode;
}
head=prevNode;
}
// Zig-Zag linkList
public static void zigZag(){
}
public static void main(String[] args) {
LinkList ll =new LinkList();
ll.addFirst(7);
System.out.println(head.data);
ll.addLast(5);
ll.addLast(3);
ll.addLast(1);
ll.printLL();
ll.addAt(4,8);
ll.printLL();
System.out.println("size : "+size);
ll.reverse();
ll.printLL();
}
}