代码随想录算法训练营Day3 | 203.移除链表元素 | 707.设计链表 | 206.反转链表

48 阅读1分钟

203.移除链表元素 leetcode.com/problems/re…

思路: 定义一个虚拟节点指向头节点,这样就能统一删除节点的操作了。

/**
 * 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) {
        if (head == null) return null;
        // 为了能统一删除节点的操作
        ListNode dummyHead = new ListNode(-1, head);
        // cur是在head前面的
        ListNode cur = dummyHead;
        while (cur.next != null) {
            if (cur.next.val == val) {
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
            
        }
        head = dummyHead.next;
        return head;
    }
}

707.设计链表 leetcode.com/problems/de…

class MyLinkedList {

    private int size;
    private ListNode head;

    public MyLinkedList() {
        size = 0;
        head = new ListNode(0);
    }
    
    public int get(int index) {
        if (size == 0) return -1;
        if (index >= size) return -1;
        ListNode cur = head;
        while (index >= 0) {
            cur = cur.next;
            index--;
        }
        return cur.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 > size) return;
        size++;
        ListNode pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        ListNode tmp = new ListNode(val);
        tmp.next = pre.next;
        pre.next = tmp;
    }
    
    public void deleteAtIndex(int index) {
        if (index >= size) return;
        size--;
        if (index == 0) {
            head = head.next;
	        return;
        }
        ListNode pre = head;
        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);
 */
 

206.反转链表 leetcode.com/problems/re…

递归做法 这个是典型的递归基础题了,递归就是要有黑盒的思想,不要一开始就扣细节,先有宏观思想,再到细节怎么做 Base case:就是只剩最后一个节点了,直接返回就行

/**
 * 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) {
        if (head == null || head.next == null) return head;
        ListNode last = reverseList(head.next);
        // 就是改变当前节点的指向嘛
        // 后面的指向当前节点
        head.next.next = head;
        // 当前节点指向左边
        head.next = null;
        return last;
    }
}