代码随想录Day03 | 203.移除链表元素 、 707.设计链表 、 206.反转链表

标题代码随想录Day03 | 203.移除链表元素 、 707.设计链表 、 206.反转链表

203.移除链表元素


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) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            if (head.val == val) {
                return null;
            }
            return head;
        }
        ListNode dummyNode = new ListNode();
        dummyNode.next = head;
        ListNode cur = dummyNode;

        //判断cur.next.val时候一定要写cur!=null和cur.next!=null
        while (cur != null && cur.next != null) {
//            System.out.println("cur" + cur.val);
//            System.out.println("cur->next" + cur.next.val);
            while (cur.next != null && cur.next.val == val) {
                cur.next = cur.next.next;
            }
            cur = cur.next;
        }
        head = dummyNode.next;

        return head;
    }
}

707.设计链表


class MyLinkedList {
    int len = 0;
    ListNode head;
    ListNode dummyNode;

    public MyLinkedList() {
        head = new ListNode();
        dummyNode = new ListNode();
        dummyNode.next = head;
    }

    public int get(int index) {
        if (!(index >= 0 && index < len)) {
            return -1;
        } else {
            ListNode temp = head;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            return temp.val;
        }
    }

    public void addAtHead(int val) {
        if (len == 0) {
            head.val = val;
            len++;
        } else {
            ListNode listNode = new ListNode();
            listNode.val = val;
            listNode.next = head;
            dummyNode.next = listNode;
            head = listNode;

            //add和delete要写len++或len--;
            len++;
        }
    }

    public void addAtTail(int val) {
        if (len == 0) {
            addAtHead(val);
        } else {
            ListNode temp = head;
//            System.out.println("addAtTail" + head.val);
            while (temp.next != null) {
                temp = temp.next;
            }
            ListNode listNode = new ListNode();
            listNode.val = val;
            temp.next = listNode;
            len++;
        }
    }

    public void addAtIndex(int index, int val) {
        if (index == len) {
            addAtTail(val);
        } else if (index == 0) {
            addAtHead(val);
        } else if (index > 0 && index < len) {
            ListNode temp = dummyNode;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            ListNode listNode = new ListNode();
            listNode.val = val;

            ListNode node = temp.next;
            listNode.next = node;
            temp.next = listNode;
            len++;
        }

    }

    public void deleteAtIndex(int index) {
        if (index >= 0 && index < len) {
            ListNode temp = dummyNode;
            for (int i = 0; i < index; i++) {
                temp = temp.next;
            }
            temp.next = temp.next.next;
            len--;
            head = dummyNode.next;

        }
    }
}

206.反转链表


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 dummyNode = new ListNode();
        dummyNode.next = head;

        ListNode p = dummyNode;
        ListNode q = head;
        ListNode r = q.next;

        while (r != null) {
            
            //判断q为head时,next需要为null
            if (q == head) {
                q.next = null;
            } else {
                q.next = p;
            }
            p = q;
            q = r;
            r = r.next;
        }
        q.next = p;
        head = q;
        return head;
    }
}
  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值