Leetcode Day3 Linked List
Published: 2024-06-07 15:33:20

##Leetcode No.203 Remove Linked List Elements
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

```
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
```


It is a normal Linked list question but i suffer some problems because of my bad coding ability.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b0ucnvyznw3qi9i0pran.png)


 But I suffer from some problem:
- the initial previous node and current node
- work for both head node and other node 

### Error Code 1
```
        while(head!=null){
            if(head.val == val){
                head = head.next;
            }

        }
        ListNode preNode = head;
        ListNode curNode = head;

        while(curNode != null){
                            System.out.println("find");
            if(curNode.val == val){
                preNode.next = curNode.next;
            }else{
                preNode = curNode;
            }
            curNode = curNode.next;
        }
        return head;  
```
- `while(head!=null)` wrong condition 


### Error Code 2
```
    public ListNode removeElements(ListNode head, int val) {
        ListNode preNode = head;
        ListNode curNode = head;


        while(curNode != null){
            if(curNode.val == val){
                // head is different from other element because it is an entry of the Linked list
                if(curNode == head){
                    head = head.next;
                    curNode = head;
                    preNode = head;
                }else{
                    // if the node is normal node we just do the remove 
                    preNode.next = curNode.next;
                }
            }else{
                preNode = curNode;
            }
            
            curNode = curNode.next;
        }
        return head;  
    }
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qimded1dwx4h67dr3es5.png)






### By modifying above problem, we achieve the right code:
```
    public ListNode removeElements(ListNode head, int val) {

        while((head != null) && (head.val == val)){
            head = head.next;
        }

        ListNode preNode = head;
        ListNode curNode = head;

        while(curNode!=null){
            if(curNode.val == val){
                preNode.next = curNode.next;
            }else{
                preNode = curNode;
            }
            curNode = curNode.next;
        }
        return head;
    }
```


- Be Careful about `while((head != null) && (head.val == val))`, I use only `while(head.val == val)` and cause a null pointer exception so that be careful about it. 




##Leetcode No.707 Design Linked List
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and next. val is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

MyLinkedList() Initializes the MyLinkedList object.
int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
void addAtHead(int val) Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
void addAtTail(int val) Append a node of value val as the last element of the linked list.
void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.


### Work for wrong code 
```
class MyLinkedList {

    public Node head;
    public Node tail;
    public int size;


    public class Node{
        public int val;
        public Node next;

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

    public MyLinkedList() {
        size = 0;
        head = null;
        tail = null;
    }
    
    public int get(int index) {
        int count = 0;
        Node cur = head;
        while(cur != null){
            if(count == index){
                return cur.val;
            }
            cur = cur.next;
            count ++;
        }
        return -1;
    }
    
    public void addAtHead(int val) {
        if(head==null){
            head = new Node(val);
            // if head == null means this list is empty
            tail = head;
        }        
        else{
            Node temp = head;
            head = new Node(val);
            head.next = temp;
        }
        size++;
    }
    
    public void addAtTail(int val) {
        if(tail==null){
            tail = new Node(val);
            head = tail;
        }
        else{
            Node temp = tail;
            temp.next = new Node(val);
            tail = temp.next;
        }  
        size++;  
    }
    
    public void addAtIndex(int index, int val) {
        if(index > size){
            return;
        }

        if(index == 0){
            addAtHead(val);
        }
        if(index == size){
            addAtTail(val);
        }

        else{
            int count = 0;
            Node pre = head;
            Node cur = head;
            while(cur != null){
                if(count == index){
                    Node insert = new Node(val);
                    pre.next = insert;
                    // important
                    insert.next = cur;
                    size++;
                    return;
                }
                else{
                    pre = cur;
                    cur = cur.next;
                    count ++;
                }
            }
        } 
    }
    
    public void deleteAtIndex(int index) {
        if(index > size){
            return;
        }

        if(index == 0){
            if(head!=null){
                head = head.next;
                if(head == null){
                    tail = null;
                }
                size--;
                return;
            }
        }else{
            int count = 0;
            Node pre = head;
            Node cur = head;
            while(cur != null){
                if(count == index){
                    pre.next = cur.next;
                    if(pre.next == null){
                        tail = pre;
                    }
                    size--;
                    return;
                }
                pre = cur;
                cur = cur.next;
                count++;
            }
            
        }
    }
}

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * MyLinkedList obj = new MyLinkedList();
 * int param_1 = obj.get(index);
 * obj.addAtHead(val);
 * obj.addAtTail(val);
 * obj.addAtIndex(index,val);
 * obj.deleteAtIndex(index);
 */
```


### Debugging...

- I might write some redundant code for the loops 
  - Because I have created a variable size, so after finishing the evaluation between the index and the size, we definitely can find the right index among the sizes. 
  - so I have to change this part?

- But I am struggling with that I am not sure where the mistake happened.
 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xmue87bbtwxhq8vy315z.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xv6zeys2ulsybc6l2pn0.png)