-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.java
52 lines (50 loc) · 867 Bytes
/
LinkedList.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
public class LinkedList
{
Node temp,temp1,head=null;
public void insert(int data){
temp=new Node(data);
if(head==null)
head=temp;
else {
temp1=head;
while(temp1.next!=null)
{
temp1=temp1.next;
}
temp1.next=temp;
}
}
public void delete(int key)
{
temp1=head;
Node previous=null;
if(temp1.data==key)
head=temp1.next;
else{
previous=temp1;
temp1=temp1.next;
while(temp1!=null&&temp1.data!=key)
{
previous=temp1;
temp1=temp1.next;
}
if(temp1!=null)
previous.next=temp1.next;
}
}
public void traverse(){
temp=head;
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.println();
}
public static void main(String s[]){
LinkedList l=new LinkedList();
l.insert(1);l.insert(2);l.insert(3);
l.traverse();
l.delete(1);
l.traverse();
}
}