Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II

Directory

  • Basic Concepts of Linked List
  • LeetCode 203. Remove Linked List Elements
  • LeetCode 707. Design Linked List
  • LeetCode 206. Reverse Linked List


Basic Concepts of Linked List

The linked list is a kind of linear structure connected by pointers. Each node is composed of two parts. One is the data field, and the other is the pointer field(storing the pointer to the next node). The pointer field of the last node points to null.
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List_第1张图片
The arrays are contiguously distributed in the memory space, but linked lists are not contiguously distributed in the memory space.

Like this linked list, the start node is 2, and the last node is 7. Each node distributes in the different address space of memory and is connected by pointers.
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List_第2张图片
Differences between array and linked list:
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List_第3张图片
The length of the array is fixed when defining. If you want to modify its length, you need to define a new array. Because when you do the insert or delete operation, you have to move every behind element forward or backward.

The length of the linked list is unfixed. It can do dynamic insert and delete operations. So linked list suits scenes that have few queries and highly frequent insert or delete.

LeetCode 203. Remove Linked List Elements

Question Link
Solution:

/**
 * 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; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        while(head != null){
            if(head.val == val)
                pre.next = head.next;
            else
                pre = head;
                
            head = head.next;
        }
        return dummy.next;
    }
}

Thoughts:

  • Removal element operation is let the next pointer of the node point directly to the next node of the next node.
  • If we want to delete the head node, we have two methods:
    • Operate the original linked list
      • If you want to remove the head node, it needs to write a separate piece of logic to handle it.
    • Set up a dummy head node
      • Handle all nodes of the original linked list by a unified method.
      • When returning, the new head node is return dummy.next

LeetCode 707. Design Linked List

Question Link

Solution:

class ListNode {
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){
        this.val = val;
    }
}
class MyLinkedList {
    int size;
    ListNode dummyHead;
    
    // initialize linked list
    public MyLinkedList() {
        size = 0;
        dummyHead = new ListNode(0);
    }
    
    public int get(int index) {
        if(index < 0 || index >=size)
            return -1;
        ListNode pre = dummyHead;
        // because there is a dummy head, so we use i<=index
        for(int i=0; i<=index; i++){
            pre = pre.next;
        }
        return pre.val;
    }
    
    public void addAtHead(int val) {
        addAtIndex(0, val);
    }
    
    public void addAtTail(int val) {
        addAtIndex(size, val);
    }
    
    public void addAtIndex(int index, int val) {
        if(index<0)
            index = 0;
        if(index>size)
            return;
        size++;
        ListNode pre = dummyHead;
        for(int i=0; i<index; i++){
            pre = pre.next;
        }
        ListNode toAdd = new ListNode(val);
        toAdd.next = pre.next;
        pre.next = toAdd;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size)
            return;
        size--;
        ListNode pre = dummyHead;
        for(int i = 0; i<index; i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
    }
}

/**
 * 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);
 */

Thoughts:

  • Don’t forget to initilize the linked list
  • Because there is a dummy head, so we use i<=index in get(index) method

LeetCode 206. Reverse Linked List

Question Link

Solution:

/**
 * 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; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode dummy = null;
        ListNode cur = head;
        ListNode pre = dummy;
        ListNode temp = null;
        while(cur != null){
            temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }
}

Thoughts

  • Specific procedures:
    • Define a cur node points to the head node. Define a null pre node
    • Save cur.next by tmp
    • Change cur.next to pre. At this time, we reversed the first node.
    • Looping this reverse logic, continue to move pre and cur
    • Finally, the cur node points to the null. Complete reverse and return the new head node pre.

你可能感兴趣的:(LeetCode,leetcode,链表)