7月8日 四道经典单链表oj题

大家好呀,本博客目的在于记录暑假学习打卡,后续会整理成一个专栏,主要打算在暑假学习完数据结构,因此会发一些相关的数据结构实现的博客和一些刷的题,个人学习使用,也希望大家多多支持,有不足之处也请指出,谢谢大家。

一,203.移除链表元素

. - 力扣(LeetCode)

方法一:定义新链表

/**
 * 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) {
        if (head == null)
            return head;
        ListNode newtail = head;
        ListNode newhead = new ListNode();
        ListNode cur = newhead;
        while (newtail != null) {
            if (newtail.val != val) {
                cur.next = newtail;
                cur = newtail;
                newtail = newtail.next;
            } else {
                if (newtail.next == null)
                    cur.next = null;
                newtail = newtail.next;
            }
        }
        return newhead.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 removeElements(ListNode head, int val) {
        if (head == null)
            return head;
        ListNode prev = head;
        ListNode pcur = head.next;
        while (pcur != null) {
            if (pcur.val == val) {
                prev.next = pcur.next;
                pcur = pcur.next;
            } else {
                prev = pcur;
                pcur = pcur.next;

            }

        }
        if(head.val==val)
        head=head.next;
        return head;
    }
}

二,206.反转链表

. - 力扣(LeetCode)

采用了把后面的节点头插到头节点前面的方式

/**
 * 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)
            return head;
        ListNode cur = head.next;
        head.next = null;
        ListNode curN;
        while (cur != null) {
            curN = cur.next;

            cur.next = head;
            head = cur;
            cur = curN;
        }
        return head;

    }
}

三,876链表的中间节点

. - 力扣(LeetCode)

运用了快慢指针

/**
 * 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 middleNode(ListNode head) {
        if(head==null)
        return head;
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null)
        {
            fast=fast.next.next;
            slow=slow.next;
        }
        return slow;

    }
}

四,面试题02.02 返回倒数第k个节点

. - 力扣(LeetCode)

采用快慢指针

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthToLast(ListNode head, int k) {
        if(head==null)
        return -1;
        ListNode fast=head;
        ListNode slow=head;
        int count=0;
        while(count!=k-1){
            fast=fast.next;
            count++;
        }
        while(fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        return slow.val;
    }
}

本期博客就到这里,感谢阅读

相关推荐

  1. 78 经典oj

    2024-07-09 21:08:03       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 21:08:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 21:08:03       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 21:08:03       58 阅读
  4. Python语言-面向对象

    2024-07-09 21:08:03       69 阅读

热门阅读

  1. 飞书文档转markdown 超级快捷方法。

    2024-07-09 21:08:03       21 阅读
  2. 达梦数据库kill会话

    2024-07-09 21:08:03       20 阅读
  3. 服务发现与注册:Eureka与Consul

    2024-07-09 21:08:03       20 阅读
  4. Vite 中怎么添加全局 scss 文件

    2024-07-09 21:08:03       25 阅读
  5. 深入理解 CSS 选择器:全面指南

    2024-07-09 21:08:03       24 阅读
  6. 记一次使用“try-with-resources“的语法导致的BUG

    2024-07-09 21:08:03       28 阅读
  7. area_center 区域和区域中心。

    2024-07-09 21:08:03       32 阅读
  8. Linux

    2024-07-09 21:08:03       23 阅读
  9. 从vs中删除自带的Microsoft Git Provider

    2024-07-09 21:08:03       18 阅读
  10. 设计模式的一点理解

    2024-07-09 21:08:03       18 阅读
  11. QT 设置控件的展开和消失

    2024-07-09 21:08:03       22 阅读
  12. qt 读取配置文件

    2024-07-09 21:08:03       21 阅读