Skip to content
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

Implementation of poll() Method for Singly Linked List #5229

Closed
srprawinraja opened this issue Jun 14, 2024 · 7 comments
Closed

Implementation of poll() Method for Singly Linked List #5229

srprawinraja opened this issue Jun 14, 2024 · 7 comments

Comments

@srprawinraja
Copy link

What would you like to Propose?

The poll() method is a new feature introduced to optimize the removal and retrieval of the head node's value in a singly linked list.

Issue details

Screenshot (119)

Additional Information

public int poll() {
if (head == null) {
return -1; // Return a default value or handle empty case
}
int headValue = head.val;
head = head.next;
return headValue;
}

@dineshlalam15
Copy link

class ListNode{
int value;
ListNode next;
public ListNode(int value, ListNode next){
this.next = next;
this.value = value;
}
public ListNode(ListNode next){
this.next = next;
}
}
public class LinkedList {
public static int poll(ListNode head){
int ans = head.value;
head = head.next;
return ans;
}
}

@sshaikshoaib
Copy link

hi there! if you allow me I would like to work on this issue

@Abinaya-Murugan
Copy link

/assign

@Khushi2309
Copy link

class ListNode {
int value;
ListNode next;

public ListNode(int value, ListNode next) {
    this.value = value;
    this.next = next;
}

}

class SinglyLinkedList {
private ListNode head;

public void addAtBegin(int value) {
    head = new ListNode(value, head);
}

public void add(int value) {
    if (head == null) {
        head = new ListNode(value, null);
    } else {
        ListNode current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = new ListNode(value, null);
    }
}

public Integer poll() {
    if (head == null) return null;
    int value = head.value;
    head = head.next;
    return value;
}

public void printList() {
    ListNode current = head;
    while (current != null) {
        System.out.print(current.value + " -> ");
        current = current.next;
    }
    System.out.println("null");
}

}

public class Main {
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();

    list.addAtBegin(30);
    list.addAtBegin(20);
    list.addAtBegin(10);
    list.printList(); // Output: 10 -> 20 -> 30 -> null

    System.out.println("Polled: " + list.poll()); // Output: Polled: 10
    list.printList(); // Output: 20 -> 30 -> null

    list.addAtBegin(5);
    list.printList(); // Output: 5 -> 20 -> 30 -> null

    System.out.println("Polled: " + list.poll()); // Output: Polled: 5
    list.printList(); // Output: 20 -> 30 -> null
}

}

@sshaikshoaib
Copy link

hi @srprawinraja I have implemented the poll method but the required checks are failling I have done code right.
could you please tell me what was the issue with my PR "shoaib/issue#5229"

@darkknightraj
Copy link

The poll() method is used to remove and return the head value of a singly linked list. To make this method robust and handle edge cases properly, we need to consider the following:

Handle the case where the list is empty (i.e., head is null).
Ensure that the head is updated correctly after the removal.
Return an appropriate value if the list is empty (e.g., -1).
Here is the implementation of the poll() method for a singly linked list:

Singly Linked List Implementation
First, we need a basic implementation of a singly linked list:
//Code

public class SinglyLinkedList {
// Define the Node class
private static class Node {
int val;
Node next;

    Node(int val) {
        this.val = val;
        this.next = null;
    }
}

// Define the head of the list
private Node head;

// Constructor to initialize the list
public SinglyLinkedList() {
    this.head = null;
}

// Method to add a new value at the end of the list
public void add(int val) {
    Node newNode = new Node(val);
    if (head == null) {
        head = newNode;
    } else {
        Node current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }
}

// Method to remove and return the head value of the list
public int poll() {
    if (head == null) {
        return -1; // Return a default value or handle the empty case
    }
    int headValue = head.val;
    head = head.next;
    return headValue;
}

// Method to print the list (for testing purposes)
public void printList() {
    Node current = head;
    while (current != null) {
        System.out.print(current.val + " -> ");
        current = current.next;
    }
    System.out.println("null");
}

public static void main(String[] args) {
    // Create a new list
    SinglyLinkedList list = new SinglyLinkedList();
    
    // Add elements to the list
    list.add(1);
    list.add(2);
    list.add(3);
    
    // Print the list
    list.printList(); // Output: 1 -> 2 -> 3 -> null
    
    // Poll elements from the list
    System.out.println(list.poll()); // Output: 1
    System.out.println(list.poll()); // Output: 2
    System.out.println(list.poll()); // Output: 3
    System.out.println(list.poll()); // Output: -1 (empty list)
    
    // Print the list after polling all elements
    list.printList(); // Output: null
}

}

@Ishantgarg-web
Copy link

This issue is already resolved. i guess
There is a method called deleteHead() that is similarly working like poll() method.
image

So, I am requesting you to please mark this issue close or provide proper description for the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants