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;
    }
}

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

  • 12
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值