Amber代码随想录打卡第三天|203.移除链表元素、707.设计链表、206.反转链表

前言

希望自己可以坚持下去吧,每天都有很多事忙现在已经快凌晨一点了很困,但继续坚持一定会有惊喜的。立一个中二的flag,我一定会跟着更完的。Fighting!

1. 203.移除链表元素

这道题老师讲的非常好,我对链表一直存在障碍看完之后完全理解了。在初期做的时候一直遇到障碍,主要是对链表的使用逻辑不清晰,用时1分钟。

/**
 * 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) {

        ListNode dummyHead = new ListNode(0,head);

        ListNode current = dummyHead;

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

        return dummyHead.next;

    }
}

2. 707.设计链表

主要遇到的困难是,不知道要怎么设计类,这个很困扰我,不知道this之类的要怎么赋值。

错误点:

1.这里的是 :index > size,而在其他的模块中判断index值是否有效的条件是if (index < 0 || index > (size-1)),这是因为在最后的位置插入val也是有效的。

 public void addAtIndex(int index, int val) {
        if (index< 0 || index > size){
            return;
        }else{
            ListNode newnode = new ListNode(val);
            ListNode current = dummy;
            while(index > 0){
                current = current.next;
                index--;
            }
            newnode.next = current.next;
            current.next = newnode;
            size++;
        }
    }

2. 应该如何设计

这里困扰了我许久,到后来发现先写add函数更加有助于理解,需要注意的是 dummy在初始化的时候需要new,否则会造成异常。通过addhead函数可以发现其实是不断的改变dummy的next值来创建链表的,不用想太复杂。

  public MyLinkedList(){
        size = 0;
        dummy = new ListNode();
    }
class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
    ListNode (int x, ListNode node){
        this.val = x;
        this.next = node;
    }
    ListNode(){

    }
}

精华笔记

3.206.反转链表

只听了卡哥的b站教程但还没有动手实践,递归法还没有听,明天试一下看看自己是不是真的理解了,感觉理解顺序很重要。

错误点:

1.ListNode tmp = current_right.next;这里是要等于current_right.next不可以是current_right,如果赋值为 current_right,那么后面在改变 current_right.next 时,tmp 也会随之改变,因为它们指向同一个对象。这样做的结果是,我们会丢失对原始链表中 current_right 后续节点的引用,从而无法继续正确地进行链表遍历和操作。换句话说,正确保留 current_right.next 的引用是为了在反转链表的过程中,不丢失对剩余部分的链表的访问。

/**
 * 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 current_left = null;
        ListNode current_right = head;
        while (true){
            ListNode tmp = current_right.next;
            current_right.next = current_left;
            current_left = current_right;
            if (tmp == null){
                return current_left;
            }
            current_right = tmp;   
        }
    }
}

2.ListNode current_left = null;ListNode current_right = head;这里的最左端要指向null,因为是终点为 null,如果初始时 current_left 设置为 head,那么在第一次迭代时,会将 head 节点的 next 指向自身,导致循环引用。

总结

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值