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

目录

203. 移除链表元素

707. 设计链表

206. 反转链表


203. 移除链表元素

难度:easy

代码随想录算法训练营day3 | 203. 移除链表元素,707. 设计链表,206. 反转链表_第1张图片

思路:

代码:

// 使用虚拟头结点
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }

        // 虚拟头结点
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode cur = head;

        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
                cur = cur.next;
            } else {
                pre = pre.next;
                cur = cur.next;
            }

        }
        return dummy.next;
    }
}

// 不使用虚拟头结点
class Solution {
    public ListNode removeElements(ListNode head, int val) {

        // 如果移除头结点
        while (head != null && head.val == val) {
            head = head.next;
        }
        // head有可能为空
        if (head == null) {
            return head;
        }

        ListNode pre = head;
        ListNode cur = head.next;

        while (cur != null) {
            if (cur.val == val) {
                pre.next = cur.next;
            } else {
                pre = pre.next;
            }
            cur = cur.next;
        }

        return head;
    }
}

707. 设计链表

707. 设计链表

难度:medium

代码随想录算法训练营day3 | 203. 移除链表元素,707. 设计链表,206. 反转链表_第2张图片

思路:

代码:

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 MyLinkedList {
    private int size;
    private ListNode dummy;
    public MyLinkedList() {
        this.size = 0;
        this.dummy = new ListNode(-1);
    }
    
    public int get(int index) {
        if (index >= size || index < 0) {
            return -1;
        }
        ListNode currentNode = dummy;
        for (int i = 0; i <= index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode.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;
        }

        ListNode currentNode = dummy;
        for (int i = 0; i < index; i++){
            currentNode = currentNode.next;
        }
        ListNode temp = currentNode.next;
        ListNode newNode = new ListNode(val, temp);
        currentNode.next = newNode;
        size++;
    }
    
    public void deleteAtIndex(int index) {
        if (index >= size || index < 0) {
            return;
        }

        ListNode currentNode = dummy;
        for (int i = 0; i < index; i++) {
            currentNode = currentNode.next;
        }
        currentNode.next = currentNode.next.next;
        size--;
    }
}

206. 反转链表

难度:easy

代码随想录算法训练营day3 | 203. 移除链表元素,707. 设计链表,206. 反转链表_第3张图片

 

思路:

代码:

class Solution {
    public ListNode reverseList(ListNode head) {

        if (head == null) {
            return null;
        }
        
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = head.next;

        while (cur != null) {
            cur.next = pre;

            pre = cur;
            cur = next;
            // 在进行最后一个反转后,next已经为null
            if (next != null) {
                next = next.next;
            }           
        }
        return pre;
    }
}

class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode pre = null;
        ListNode cur = head;
        ListNode next = null;

        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }

        return pre;
    }
}

你可能感兴趣的:(代码随想录,#,链表,算法,链表,数据结构)