Badar Shahzad Khan Badar Shahzad Khan - 1 year ago 69
Java Question

Unable to delete first item in linked list

I saw some solutions, but I am still unable to resolve the error in my code. My

method is not removing any elements from the list. Both invocations of
produce the same output. Can you tell me what I am missing, or where the error is?


package lab5;

public class LinkedList {

public static void main(String argsp[]){

List ob = new List();

ob.addAtStart("y", 6);
ob.addAtStart("w", 4);
ob.addAtStart("z", 3);






package lab5;

public class List {

Node head;

public List(){

public List(Node e){

Node oldfirst=null;
Node lasthead=null;

public void addAtStart(String name, int age){
Node newObject= new Node(name,age);;

if (oldfirst==null) {
oldfirst = newObject;
head = newObject;
lasthead = head;

public void display() {
Node store = head;
while (store != null) {

public void addAtEnd(String name, int age){
Node atEndValue = new Node(name, age); = atEndValue;
oldfirst = atEndValue;

public void deleteFromStart() {
while (lasthead != null) {
lasthead =;

public boolean isEmpty() {
return head == null;


package lab5;

public class Node {

String name;
int age;
Node next;

public Node(){

public Node(String name, int age ){;
next = null;

public void display(){
System.out.println("Name: " + name + " Age: " + age);

Answer Source

tl;dr To remove the first element in a linked-list:

head =

When you're implementing a singly-linked list, you really only need to keep a single pointer: head (i.e. a reference to the first node in the list. In practice, it's also useful to keep track of the last element in the list (commonly referred to as tail). This allows constant-time operations at the end of the list, which are useful if you're frequently adding/removing elements at the end. So, with this basic implementation, you end up with something like this:

class LinkedList {
    private Node head = null;
    private Node tail = null;

    public LinkedList() {}

    public LinkedList(Node e) {
        head = e;
        tail = e;

class Node {
    Node next = null;
    // other data

Adding and removing elements in a linked list boils to down to updating what the head and tail variables are referring to. Consider a singly-linked list with three elements, [A, B, C]. The values head and tail values align like this:

 A -> B -> C -> null
 ^         ^
 |         |
head      tail

If you want to insert a new element, X, there are two steps:

1) Tell to refer to A:

X -> A -> B -> C -> null
     ^         ^
     |         |
    head      tail

2) Update head to refer to X:

 X -> A -> B -> C -> null
 ^              ^
 |              |
head           tail

You move head and tail around in similar fashion, depending on whether you're adding or removing, and whether or not the operation is at the beginning or end of the list.

Removing an element from the start (assuming that the list is not empty) is as simple as updating head to refer to the next element. In our example above, this would mean moving head to refer to, which is A:

X -> A -> B -> C -> null
     ^         ^
     |         |
    head      tail

Now remember, the linked list is only directly aware of head and tail, so once you update head to refer to A there is nothing referencing X anywhere in your application, and it has effectively been deleted (in Java this will cause it to be garbage-collected).

Effectively what we did above was simply head = Again, you'll have to ensure the list isn't empty first, since will cause a null pointer exception if the list is empty.

I'd also suggest removing oldfirst and lasthead, and updating your add* methods based on the theory above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download