链表1|203.移除链表元素 |707.设计链表 |206.反转链表

链表1|203.移除链表元素 |707.设计链表 |206.反转链表


一、链表理论基础

java就有自己的内存回收机制,删除节点时不用自己手动释放。 Java链表的定义如下:

public class ListNode {
    // 结点的值
    int val;
    // 下一个结点
    ListNode next;
    // 节点的构造函数(无参)
    public ListNode() {
    }
    // 节点的构造函数(有一个参数)
    public ListNode(int val) {
        this.val = val;
    }
    // 节点的构造函数(有两个参数)
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

二、203.移除链表元素

题目连接:203. 移除链表元素 - 力扣(LeetCode)

  1. 删除元素必须得有前驱,若删除的是头节点的下一个节点,临时节点 cur = head , 删除时 cur.next = cur.next.next

  2. 使用虚拟头节点的思想,使对链表中每个元素进行删除时的操作统一,此方法需要新建一个cur指针来遍历链表,为了保留头节点,防止在遍历过程中改变head(head是需要用来return的)。

  3. 虚拟头节点返回的是 dummy.next ,而不是head,因为head在遍历的过程中可能已经删除了

public class Solution {
    class ListNode {
        int val;
        ListNode next;
        public ListNode (){}
        public ListNode(int val){
            this.val = val;
        }
        public ListNode(int val, ListNode next){
            this.val = val;
            this.next = next;
        }
    }
//  暴力解法  不使用虚拟头节点的方法
    public ListNode removeElements1(ListNode head, int val) {
        while(head != null && head.val == val ){
            head = head.next;
        }
        if(head == null)  return head;
        ListNode cur = head;
        while(cur != null && cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }
        return head;
    }

    // 使用虚拟头节点的方式
    public ListNode removeElements2(ListNode head, int val) {
        if(head == null) return head;
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode cur = dummy;
        while(cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return dummy.next;
    }  
}

三、707.设计链表

题目连接:707. 设计链表 - 力扣(LeetCode)

  1. 添加节点注意赋值的顺序,添加节点和删除节点一样需要找到添加位置的前一个节点,进行添加操作。

  2. 注意对于插入删除节点 index 的判断,因为 index 是从0开始的,获取第 index 个节点的值循环条件index>=0,如果是获取头节点需要等于0的条件,不能是index>0,注意和上题不一样!!!这里定义的head是虚拟头节点。写着写着就把这个当作真头节点了。

  3. 每当进行添加、删除操作时,要注意对size值进行 ++ 和 – 操作,不然会引起 get(int index) 的空指针报错,找了好久错误才发现。

//定义链表节点
class Node{
    int val;
    Node next;
    Node(){}
    Node(int val){ this.val = val;}
}
public class MyLinkedList {
    int size; //链表节点的个数
    Node head; // 虚拟头节点

    // 初始化链表
    public MyLinkedList() {
        size = 0;
        head = new Node(0);
    }
    //获取链表中第 index 个节点的值。如果索引无效,则返回-1
    public int get(int index) {
        if(index < 0 || index >= size) return -1;
        Node cur = head;
        while(index >= 0){
            cur = cur.next;
            index--;
        }
        return cur.val;
    }

    //在链表的第一个元素之前添加一个值为 val 的节点。
    public void addAtHead(int val) {
        addAtIndex(0,val);
    }

    //将值为 val 的节点追加到链表的最后一个元素。
    public void addAtTail(int val) {
        addAtIndex(size,val);
    }

    //在链表中的第 index 个节点之前添加值为 val  的节点。
    //如果 index 等于链表的长度,则该节点将附加到链表的末尾。
    // 如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
    public void addAtIndex(int index, int val) {
        if(index > size) return;
        if(index < 0){
            index = 0;
        }
        size++;
        Node temp = head;
        for(int i = index; i > 0; i--){
            temp = temp.next;
        }
        Node cur = new Node(val);
        cur.next = temp.next;
        temp.next = cur;
    }

    //如果索引 index 有效,则删除链表中的第 index 个节点。
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size) return;
        size--;
        if(index == 0){
            head = head.next;
            return;
        }
        if(index > 0 && index < size){
            Node cur = head;
            for(int i = index; i > 0; i--){
                cur = cur.next;
            }
            cur.next = cur.next.next;
        }
    }
}

四、206.反转链表

题目连接:206. 反转链表 - 力扣(LeetCode)

  1. 双指针法:反转链表采用双指针思路理解起来还是比较简单,注意用cur和pre 进行反转操作的时候,他们与临时指针temp进行赋值的顺序,这里容易写错,记住在反转之前用临时指针记录下cur的下一个节点,为了防止下个节点的丢失。
  2. 递归法:其思想上与双指针思想一致,只是在实现上采用的递归方式。每一次调用reverser(ListNode cur, ListNode pre)函数都是将 cur 和 pre 沿链表反转前的链表方向移动了一位。
双指针写法如下:
class ListNode {
    int val;
    ListNode next;
    public ListNode (){}
    public ListNode(int val){
        this.val = val;
    }
    public ListNode(int val,ListNode next){
        this.val = val;
        this.next = next;
    }
}
public class Solution {
        // 双指针写法
        public ListNode reverseList1(ListNode head) {
            ListNode cur = head;
            ListNode pre = null;
            while(cur != null){
                ListNode temp = new ListNode();
                temp = cur.next;
                cur.next = pre;
                pre = cur;
                cur = temp;
            }
            return pre;
        }
递归写法如下:
class ListNode {
    int val;
    ListNode next;
    public ListNode (){}
    public ListNode(int val){
        this.val = val;
    }
    public ListNode(int val,ListNode next){
        this.val = val;
        this.next = next;
    }
}
public class Solution3 {
        //递归写法
    public ListNode reverse(ListNode cur, ListNode pre){
            if(cur == null) return pre;
            ListNode temp = new ListNode();
            temp = cur.next;
            cur.next = pre;
            return reverse(temp,cur);
    }

    public ListNode reverseList(ListNode head) {
        return reverse(head,null);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值