My Solution - Using LL


  • -1
    A

    class MyQueue {
    // Push element x to the back of queue.
    Node head = null;
    Node tail = null;
    public void push(int x) {
    if(head == null){
    head = new Node(x);
    tail = head;
    }else{
    tail.next = new Node(x);
    tail = tail.next;
    }
    }

    // Removes the element from in front of queue.
    public void pop() {
        if(head!=null){
            head = head.next;
        }        
    }
    
    // Get the front element.
    public int peek() {
        return head.val;
    }
    
    // Return whether the queue is empty.
    public boolean empty() {
        return head==null;
    }
    

    }

    class Node{
    int val;
    Node next;

    public Node(int c){
        val = c;
        next = null;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.