LeetCode刷题day02||203.移除链表元素&&707.设计链表&&206.反转链表--链表

203.移除链表元素

题目描述

在这里插入图片描述
题目链接

思路分析

删除节点的方式
在这里插入图片描述

删除头节点的两种方式

  • 不添加虚拟节点
    将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。
    在这里插入图片描述
    在这里插入图片描述

  • 添加虚拟节点
    虚拟头结点代替原先的头节点
    在这里插入图片描述

代码

注释 – 单向链表的定义

/**
 * 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) {
        //头部一直删除
        while(head != null && head.val == val){
            head = head.next;
        }
        ListNode curr = head;
        while(curr != null){
            while(curr.next != null && curr.next.val == val){
                curr.next = curr.next.next;
            }
            curr = curr.next;
        }
        return head;

    }
}
  • 添加虚拟节点
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return head;
        }
        ListNode dummy = new ListNode(-1,head);
        ListNode pre = dummy;
        ListNode curr = head;
        while(curr != null){
            if(curr.val == val){
                pre.next = curr.next;
            }else{
                pre = curr;
            }
            curr = curr.next;
        }
        return dummy.next;

    }
}

707.设计链表

题目描述

在这里插入图片描述
题目链接

思路分析

分为单链表和双链表方式

  • 单链表
    注意size-- 的位置,return后面语句不能执行
    //删除第index个节点
    public void deleteAtIndex(int index) {
        if (index < 0 || index >= size) {
            return;
        }
        size--;
        if (index == 0) {
            head = head.next;
	        return;
        }
        ListNode pred = head;
        for (int i = 0; i < index ; i++) {
            pred = pred.next;
        }
        pred.next = pred.next.next;
    }
}
  • 双向链表
    插入方式,每个节点要存储本身的值,后继节点和前驱节点。除此之外,需要一个哨兵节点作为头节点 head 和一个哨兵节点作为尾节点 tail。
    在这里插入图片描述

代码

  • 单链表
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) {
        //如果index非法,返回-1
        if (index < 0 || index >= size) {
            return -1;
        }
        ListNode currentNode = head;
        //包含一个虚拟头节点,所以查找第 index+1 个节点
        for (int i = 0; i <= index; i++) {
            currentNode = currentNode.next;
        }
        return currentNode.val;
    }
    
    public void addAtHead(int val) {
        
        ListNode newNode = new ListNode(val);
        //注意顺序不能颠倒
        newNode.next = head.next;
        head.next = newNode;
        size++;
    }
    public void addAtTail(int val) {
        ListNode currentNode = head;
        for(int i = 0;i < size;i++){
            currentNode = currentNode.next;
        }
        currentNode.next = new ListNode(val);
        size++;

    }
    public void addAtIndex(int index, int val) {
        if(index > size){
            return;
        }
        if(index < 0){
            index = 0;
        }
        size++;
        ListNode currentNode = head;
        for(int i = 0; i < index;i++){
            currentNode = currentNode.next;
        }
        ListNode newNode = new ListNode(val);
        newNode.next = currentNode.next;
        currentNode.next = newNode;
        
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }
        ListNode currentNode = head;
        for(int i = 0;i < index;i++){
            currentNode = currentNode.next;
        }
        currentNode.next = currentNode.next.next;
        size--;    
    }
}
  • 双链表
class LinkNode{
    int val;
    LinkNode pre,next;
    LinkNode(){};
    LinkNode(int val){
        this.val = val;
    }

}
class MyLinkedList {
    int size;
    LinkNode head,tail;
    public MyLinkedList() {
        size = 0;
        head = new LinkNode(0);
        tail = new LinkNode(0);
        //不初始化,null指针异常
        head.next = tail;
        tail.pre = head;
    }
    
    public int get(int index) {
        if(index >= 0 && index < size){
            LinkNode cur = head;
            if(index >= size / 2){//从尾部开始找
                cur = tail;
                for(int i = 0;i < size - index;i++){
                    cur = cur.pre;
                }
            }else{//从头部开始找
                for(int i = 0;i <= index;i++){
                    cur = cur.next;
                }
            }
            return cur.val;
        }
        return -1;

    }
    
    public void addAtHead(int val) {
        LinkNode newNode = new LinkNode(val);
        newNode.next = head.next;
        newNode.pre = head;
        head.next = newNode;
        newNode.next.pre = newNode;
        size++;
    }
    
    public void addAtTail(int val) {
        LinkNode newNode = new LinkNode(val);
        newNode.next = tail;
        newNode.pre = tail.pre;
        tail.pre = newNode;
        newNode.pre.next = newNode;
        size++;
    }
    
    public void addAtIndex(int index, int val) {
        if(index > size){
            return;
        }
        LinkNode pre = head;
        for(int i = 0;i < index;i++){
            pre = pre.next;
        }
        LinkNode newNode = new LinkNode(val);
        newNode.next =  pre.next;
        newNode.pre = pre;
        pre.next = newNode;
        newNode.next.pre = newNode;
        size++;
    }
    
    public void deleteAtIndex(int index) {
        if(index < 0 || index >= size){
            return;
        }
        LinkNode pre = head;
        for(int i = 0;i < index;i++){
            pre = pre.next;
        }
        pre.next = pre.next.next;
        pre.next.pre = pre; 
        size--;

    }
}

206.反转链表

题目描述

在这里插入图片描述
题目链接

思路分析

  • 双指针
    • 首先定义一个cur指针,指向头结点,再定义一个pre指针,初始化为null。
    • 然后就要开始反转了,首先要把 cur->next 节点用tmp指针保存一下,也就是保存一下这个节点。
    • 走循环不断移动两个指针
  • 递归
    思想类似双指针

代码

  • 双指针
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode temp = null;
        ListNode pre = null;
        ListNode curr = head;
        while(curr != null){
            //暂存
            temp = curr.next;
            curr.next = pre;
            pre = curr;
            curr = temp;
        }
        return pre;

    }
}
  • 递归
class Solution {
    public ListNode reverseList(ListNode head) {
        return reverse(null,head);
    }
    public ListNode reverse(ListNode pre,ListNode curr){
        if(curr == null){
            return pre;
        }
        ListNode temp = null;
        temp = curr.next;
        curr.next = pre;
        //类似于双指针的
        //pre = curr;
        //curr = temp;
        return reverse(curr,temp);
    }
}

从后往前递归

// 从后向前递归
class Solution {
    ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode cur = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return cur;
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值