随想录刷题第三天 | 203.移除链表元素 707.设计链表 206.反转链表

203.移除链表元素
遍历删除就完了,考虑到头节点也可能被删除所以可以添加一个虚拟头节点,不过我这里是对头结点做的单独处理,我觉得这样可以节约些空间

    public ListNode removeElements(ListNode head, int val) {
        if(head == null) return null;

        ListNode parent = head;
        while(parent.next != null){
            ListNode children = parent.next;
            if(children.val == val){
                parent.next = children.next;
            }else {
                parent = parent.next;
            }
        }

        //最后判断头节点是否需要删除
        //如果头节点需要删除就加一个虚拟节点
        if(head.val == val){
            if(head.next == null) return null;
            head = head.next;
        }

        return head;
    }

707.设计链表
用单链表实现的,特殊的地方就是加个len记录一下当前链表长度

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

    ListNode head;
    int len;

    public MyLinkedList() {
        this.head = new ListNode();
        this.len = 0;
    }
    
    public int get(int index) {
        if(index >= this.len){
            return -1;
        }
        int i=0;
        ListNode cur = this.head.next;
        while(i < index){
            cur = cur.next;
            i++;
        }
        return cur.val;
    }
    
    public void addAtHead(int val) {
        ListNode node = new ListNode(val);
        node.next = this.head.next;
        this.head.next = node;
        this.len++;
    }
    
    public void addAtTail(int val) {
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }

        ListNode node = new ListNode(val);
        cur.next = node;

        this.len++;
    }
    
    public void addAtIndex(int index, int val) {
        if(index > this.len){
            return;
        }
        int i=-1;
        ListNode cur = this.head;
        while(i < index-1){
            cur = cur.next;
            i++;
        }
        ListNode node = new ListNode(val);
        if(cur.next != null) node.next = cur.next;
        cur.next = node;
        this.len++;
    }
    
    public void deleteAtIndex(int index) {
        if(index >= this.len){
            return;
        }
        int i=-1;
        ListNode parent = this.head;
        while(i < index-1){
            parent = parent.next;
            i++;
        }
        parent.next = parent.next.next;
        this.len--;
    }
}

206.反转链表
这是一道很经典的题目我之前竟然没做过,第时间想法就是先遍历找到尾节点然后往回翻转。
链表题,最好是画一下图,这样更容易理解。

    public ListNode reverseList(ListNode head) {
        if(head == null ) return head;

        ListNode cur = null;
        ListNode priv = head;
        while(priv.next != null){
            ListNode t = priv.next;
            priv.next = cur;
            cur = priv;
            priv = t;
        }
        priv.next = cur;

        return priv;
    }

看到其他答案的递归写法这里也学习记录一下

    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;

        //翻转后的首节点
        ListNode ret = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return ret;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容