-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeletionFromALinkedList.java
56 lines (49 loc) · 1.18 KB
/
DeletionFromALinkedList.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
package algo.linkedlist;
import org.junit.Assert;
import org.junit.Test;
public class DeletionFromALinkedList
{
@Test
public void test()
{
LLNode n10 = new LLNode(10,null);
LLNode n9 = new LLNode(9,n10);
LLNode n8 = new LLNode(8,n9);
LLNode n7 = new LLNode(7,n8);
LLNode n6 = new LLNode(6,n7);
LLNode n5 = new LLNode(5,n6);
LLNode n4 = new LLNode(4,n5);
LLNode n3 = new LLNode(3,n4);
LLNode n2 = new LLNode(2,n3);
LLNode head = new LLNode(1,n2);
LLNode newHead = deleteFromLinkedList(5,head);
Assert.assertEquals(6,newHead.next.next.next.next.item);
newHead = deleteFromLinkedList(1,head);
Assert.assertEquals(2,newHead.item);
newHead = deleteFromLinkedList(1,null);
Assert.assertNull(newHead);
newHead = deleteFromLinkedList(1,n10);
Assert.assertNull(newHead);
}
LLNode deleteFromLinkedList(int elem, LLNode head)
{
if(head==null || head.next==null)
{
return null;
}
if(head.item==elem)
{
return head.next;
}
LLNode n = head;
while(n.next!=null)
{
if(n.next.item==elem)
{
n.next = n.next.next;
}
n = n.next;
}
return head;
}
}