mgaw

导航

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

目录

203 移除链表元素

带虚拟头节点

不带虚拟头节点

不带pre节点

206 反转链表

递归

迭代

707 设计链表

单链表


203 移除链表元素

如果想删除链表中的一个元素,需要先找到它的上一个节点,然后再进行删除操作。

可以通过设置虚拟头节点的方式将头节点与其他节点统一起来进行操作,也可以先判断头节点的值确保头节点的值不为val。

带虚拟头节点

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode ln = new ListNode(-1,head);
        ListNode pre = ln;
        ListNode cur = head;
        while(cur != null){
            if(cur.val == val)pre.next = cur.next;
            else pre = cur;
            cur = cur.next;
        }
        return ln.next;
    }
}

不带虚拟头节点

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        while(head != null && head.val == val)head = head.next;
        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 = cur;
            cur = cur.next;
        }
        return head;
    }

不带pre节点

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


206 反转链表

递归

class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null,head);
    }
    public ListNode reverse(ListNode pre,ListNode cur){
        if(cur == null)return pre;
        ListNode tmp = cur.next;
        cur.next = pre;
        
        return reverse(cur,tmp);
    }
}

迭代

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

​

707 设计链表

单链表

class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNode(int val){this.val = val;}
}

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

双链表

posted on 2023-10-27 20:20  A魔法恐龙  阅读(9)  评论(0)    收藏  举报  来源