Tags » Linked List

Linked List - Remove Linked List Elements (LeetCode)

LeetCode 203 Linked List — Easy

Remove all the elements from a linked list of integers that have value val.

Example
Given:  1 —> 2 —> 6 —> 3 —> 4 —> 5 —> 6, val = 6… 104 more words

Java

Stack Implementation Using Linked List

/* Implementation of Stack using Linked List */

#include<bits/stdc++.h>
using namespace std;
class Stack{
private:
    struct Node{
        int info;
        struct Node *next;
        int counter = 0;
    };
    Node *head;
public:
    Stack(){

        head = NULL;
    }
    void Push(int item);
    void Pop();
    int top();
    int Size();
};
void Stack::Push(int item){
    Node *temp;
    temp = new Node;
    temp->info=item;
    if(head ==NULL){
        head = temp;
        head->next=NULL;
        head->counter=1;
    }else{
        temp->next=head;
        temp->counter=head->counter+1;
        head = temp;
    }
}
int Stack::top(){
    if (head==NULL){
        cout<<"Stack Is Empty";
    }else{
        return head->info;
    }
}
void Stack::Pop(){
    if(head==NULL){
        cout<<"Stack is Empty";
    }else{
        Node *del;
        del = head;
        head=head->next;
        delete(del);
    }
}
int Stack::Size(){
    if(head==NULL)
        return 0;
    else
        return head->counter;
}
int main(){
    Stack stk;
    while(1){
        cout<<"1.Push"<<endl
            <<"2.Pop"<<endl
            <<"3.Top"<<endl
            <<"4.Size"<<endl
            <<"5.Exit";
        cout<<endl<<"Insert Choice# ";
        int c;
        cin>>c;
        switch(c){
        case 1:
            int val;
            cout<<"Value: ";
            cin>>val;
            stk.Push(val);
            break;
        case 2:
            stk.Pop();
            break;
        case 3:
            cout<<"Top of Stack: "<<stk.top()<<endl;
            break;
        case 4:
            cout<<"Size of Stack: "<<stk.Size()<<endl;
            break;
        case 5:
            return 0;
        }
        cout<<endl;
    }
}
Miscellaneous

Linked List Problems Common Strategies

Linked List is one of the most common data structures that appear in algorithm contests, interviews, class assignments and other scenarios. They are usually interesting and worth some thinking before you can come up with good ways to solve them. 970 more words

Tech

Linked List - Reverse Linked List (LeetCode)

LeetCode 206 Linked List — Easy

Reverse a singly Linked List. Can you do it iteratively and recursively?

Idea

Iteratively

To solve the problem iteratively, you need three pointers, tail, middle, lead. 173 more words

Java

Linked List - Palindrome Linked List (LeetCode)

LeetCode 234 Linked List, Two Pointers — Easy

Given a singly linked list, determine if it is a palindrome. Do it in O(n) time and O(1) space. 240 more words

Java

Linked List - Delete Node in a Linked List (LeetCode)

LeetCode 237 – Linked List – Easy

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 132 more words

Java

Merge Two Sorted List

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 83 more words

CodeBlocks