第二章 链表part01

57 阅读2分钟

203. Remove Linked List Elements

leetcode.com/problems/re…

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.

题目解析:

  • 虚拟头结点,确保原链表的所有节点就都可以按照统一的方式进行移除了

代码:

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode prev = dummy;
        while(head != null) {
            if (head.val == val) {
                prev.next = head.next;
            } else {
                prev = head;
            }
            head = head.next;
        }
        return dummy.next;
    }
}

707. Design Linked List

leetcode.com/problems/de…

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 nextval 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.

题目解析:

  • 使用头节点和尾节点代表链表的头部和尾部,是用size代表链表的大小可以减少时间复杂度
  • 需要考虑插入/删除头部、尾部、中间三种不同的情况

代码:

class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
    }
}
class MyLinkedList {
    ListNode head;
    ListNode tail;
    int size = 0;
    public MyLinkedList() {
        
    }
    
    public int get(int index) {
        ListNode newHead = head;
        for (int i = 0; i < index; i++) {
            if (newHead != null) {
                newHead = newHead.next;
            } else {
                return -1;
            }
        }
        return newHead != null ? newHead.val : -1;        
    }
    
    public void addAtHead(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            tail = node;
        } else {
            node.next = head;
            head = node;
        }
        size++;
    }
    
    public void addAtTail(int val) {
        ListNode node = new ListNode(val);
        if (tail == null) {
            head = node;
            tail = node;
        } else {
            tail.next = node;
            tail = node;
        }
        size++;
    }
    
    public void addAtIndex(int index, int val) {
        ListNode node = new ListNode(val);
        if (index == 0) {
            addAtHead(val);
        } else if (index == size) {
            addAtTail(val);
        } else if (index > size) {
            return;
        } else {
            ListNode newHead = head;
            for (int i = 0; i < index - 1; i++) {
                newHead = newHead.next;
            }
            ListNode temp = newHead.next;
            newHead.next = node;
            node.next = temp;
            size++;
        }
    }
    
    public void deleteAtIndex(int index) {
        if (index < size) {
            if (index == 0) {
                head = head.next;
            } else {
                ListNode newHead = head;
                for (int i = 0; i < index - 1; i++) {
                    newHead = newHead.next;
                }
                newHead.next = newHead.next.next;
                if (newHead.next == null) {
                    tail = newHead;
                }
            }
            size--;
        }
    }
}

206. Reverse Linked List

leetcode.com/problems/re…

Given the head of a singly linked list, reverse the list, and return the reversed list.

题目解析:

  • 双指针或者递归都能实现反转

代码
双指针法

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head != null) {
            ListNode pre = null;
            while (head.next != null) {
                ListNode temp = head.next;
                head.next = pre;
                pre = head;
                head = temp;
            }
            head.next = pre;
        }
        return head;
    }
}

递归法

class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null, head);
    }

    public ListNode reverse(ListNode prev, ListNode cur) {
        if (cur != null) {
            ListNode temp = cur.next;
            cur.next = prev;
            return reverse(cur, temp);
        }
        return prev;
    }
}

总结

链表问题的思路:

  • 虚拟节点:当前头节点可能发生变化时可能需要虚拟头结点
  • 双指针:操作整条链表时可以考虑
  • 递归:操作整条链表时可以考虑

以上几种方法可能需要结合使用。