package com.vav.CTCI.Common.LinkedList;

/**
 * Created by Vaibhav on 5/6/2017.
 */
public class LinkedList<T> {
    private Link head;
    public LinkedList(){
        head=null;
    }
    public void insertAtEnd(T data){
        Link<T> node = new Link(data);
        node.setNext(null);
        if(head==null){
            head = node;
            return;
        }
        Link current = head;
       while(current.getNext()!=null){
           current = current.getNext();
       }
       current.setNext(node);
    }
    public void insertAtStart(T data){
        Link<T> node = new Link(data);
        node.setNext(null);
        if(head==null){
            head = node;

        }else{
            node.setNext(head);
            head = node;
        }
    }
    public void insertAtPosition(T data, int position){
        Link<T> node = new Link(data);
        node.setNext(null);
        Link current = head;
        Link previous = null;
        int count=0;
        if(head == null){
            head = node;
            return;
        }
        if(position ==0){
            node.setNext(head);
            head =node;
            return;
        }
        while(current!=null && count<position){
            previous = current;
            current = current.getNext();
            count++;
        }
        previous.setNext(node);
        node.setNext(current);
    }
    public void printList(){
        Link current = head;
        while(current!=null){
            System.out.print(current.getData()+"->");
            current=current.getNext();
        }
    }
    public void deleteAtPosition(int position){
        if(head==null){
            System.out.println("List is empty!");
            return;
        }
        if(position==0){
            head = head.getNext();
            return;
        }
        Link current=head,previous=null; int count=0;
        while(current.getNext()!=null &&count<position){
            previous = current;
            current = current.getNext();
            count++;
        }
        previous.setNext(current.getNext());
    }
    public Link getHead(){
        return head;
    }
    public void setHead(Link head){
        this.head = head;
    }

}

@@ -2073,32 +2073,676 @@
 tNext());%0A    %7D%0A
+    //Works for integers%0A    public Link deleteWithValue(T value)%7B%0A        Link current = head;%0A        Link previous = current;%0A        //this may not work for strings or objects%0A        if(head.getData().equals(value))%7B%0A            head = head.getNext();%0A            return head;%0A        %7Delse%7B%0A            current = current.getNext();%0A        %7D%0A        while (current.getData()!=null)%7B%0A            if(current.getData().equals(value))%7B%0A                previous.setNext(current.getNext());%0A                return head;%0A            %7D%0A            previous = current;%0A            current = current.getNext();%0A        %7D%0A        return head;%0A    %7D%0A
     public Link 

Delete an item