-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLexample1.java
More file actions
65 lines (57 loc) · 1 KB
/
LLexample1.java
File metadata and controls
65 lines (57 loc) · 1 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
package practice1;
import java.util.*;
import edu.princeton.cs.introcs.StdIn;
public class LLexample1 {
public static void head2null(Node head)
{
head=null;
return;
}
public static Node getNthNode(Node head,int n)
{
Node itr=head;
int cnt=0;
while(itr!=null)
{
if(cnt==n) break;
else
{
cnt++;
itr=itr.next;
}
}//while
return itr;
}
public static Node delete(Node head, int d)
{
if(head==null) return head;
if(head.data==d) return head.next;
Node itr=head;
while(itr.next!=null)
{
if(itr.next.data==d)
{
itr.next=itr.next.next;
return head;
}
itr=itr.next;
}
return head;
}
public static void main(String[] args)
{
LinkedList ll= new LinkedList();
int n=StdIn.readInt();
for(int i=0;i<n;i++) ll.addToHead(i);
ll.reverse();
head2null(ll.head);
System.out.println("After head2null");
ll.printList();
for(int i=0;i<10;i++)
{
int data=StdIn.readInt();
ll.head=delete(ll.head,data);
ll.printList();
}
}
}