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

本文介绍了如何在链表中移除指定元素,包括直接操作和设置虚拟头结点两种方法,并展示了设计链表的数据结构以及反转链表的双指针法和递归法解法。
摘要由CSDN通过智能技术生成

203.移除链表元素

题目链接:203.移除链表元素

思路1.直接在原链表上进行删除操作、2.设置虚拟头结点进行删除操作

解法1:直接操作

首先采用while循环判断头结点不为空且头结点处值为输入值,头结点指针后移一位;

定义新指针cur指向头结点,while循环判断新指针cur不为null且cur指针指向的下一位不为null,当cur.next指向的值为输入值时,cur.next.next的地址赋值给cur.next实现删除操作。否则cur向后移动一位。

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

解法2:设置虚拟头节点

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;
            } else {
                pre = cur;
            }
            cur = cur.next;
        }
        return dummy.next;
    }
}

707.设计链表

题目链接:707.设计链表

思路:1.直接在原链表上进行删除操作、2.设置虚拟头结点进行删除操作

解法1:直接操作

解法2:设置虚拟头结点

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;
        }
        if (index < 0) {
            index = 0;
        }
        size++;
        ListNode pre = head;
        for (int i = 0; i < index; i++) {
            pre = pre.next;
        }
        ListNode add = new ListNode(val);
        add.next = pre.next;
        pre.next = add;
    }

    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;
    }
}

206.反转链表

题目链接:206.反转链表

思路:1.双指针法、2.递归法

解法1:双指针法

定义当前结点前一个结点指针pre,当前结点指针cur和中转指针temp用于保存结点。

temp保存cur.next结点,通过cur.next = pre改变cur.next的指向,cur指针向后移动一位,pre指针也向后移动一位。

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

解法2:递归法

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

    private ListNode reverse(ListNode pre, ListNode cur) {
        if (cur == null) {
            return pre;
        }
        ListNode temp = null;
        temp = cur.next;
        cur.next = pre;
        return reverse(cur, temp);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值