DEV Community

Flame Chan
Flame Chan

Posted on • Edited on

Leetcode Day3 Linked List

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; }
 * }
 */
Enter fullscreen mode Exit fullscreen mode

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

Image description

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;  
Enter fullscreen mode Exit fullscreen mode
  • 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;  
    }
Enter fullscreen mode Exit fullscreen mode

Image description

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;
    }
Enter fullscreen mode Exit fullscreen mode
  • 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);
 */
Enter fullscreen mode Exit fullscreen mode

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

Image description

Top comments (4)

Collapse
 
thaisavieira profile image
Thaísa Vieira

Woah, what a great post, Flame Chan! Thanks for contributing to our community's rich content. Also, I love to read about the journey of how the debbug was made and what are you learning. Keep doing your awesome work!

Collapse
 
flame_chan_llll profile image
Flame Chan

Thanks for your kind reply!!! As a Leetcode beginner, I'm still struggling with this lol, I will continue to work hard and update my study repo~

Collapse
 
thaisavieira profile image
Thaísa Vieira

I'm always here to support learning cause I'm a beginner too, and I know how hard the journey is. Usually, I study with HackerRank challenges but the struggle is the same. Let's stay in touch!

Thread Thread
 
flame_chan_llll profile image
Flame Chan

Thank u so much!! It's great to know I'm not alone in this journey. Let's definitely stay in touch and keep encouraging each other!