Skip to content

Hacktoberfest 2022 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions LinkedListDeletions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.anurag;

public class LL {

private Node head;
private Node tail;

private int size;

public LL() {
this.size = 0;
}

//----------------DELETION----------------->>>>>>>>>>
//DELETE ELEMENT AT FIRST
public int deleteFirst(){
int val = head.value;
head = head.next;

if(head == null){
tail = null;
}

size--;

return val;
}

//DELETE ELEMENT AT LAST
public int deleteLast(){
if(size <= 1){
return deleteFirst();
}

Node secondLast = get(size - 2);

int val = tail.value;
tail = secondLast;
tail.next = null;

return val;
}

//DELETE ELEMENT AT A PARTICULAR INDEX
public int delete(int index){
if(index == 0){
return deleteFirst();
}

if(index == size-1){
return deleteLast();
}

Node prev = get(index - 1); //prev is the previous element from the element that's to be removed
int val = prev.next.value;

prev.next = prev.next.next;

return val;
}




}