力扣刷题-链表


前言

本博客仅做学习笔记,如有侵权,联系后即刻更改

科普:


参考网址

链表

在这里插入图片描述

知识点

单链表

链表中的每个节点只包含一个指针域

  • 单链表的第一个节点的存储位置叫做头指针
  • 最后一个节点的后继指针为空,一般用 NULL 或者 “^” 表示。
    在这里插入图片描述

双向链表

相比起单链表来说

  • 多了一个前驱指针 prev,指向前驱节点

头指针

链表的必备元素且无论链表是否为空,不能为空

后继指针

下个节点地址的指针

力扣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 current = head;
        while(current != null) {
            while(current.next != null && current.next.val == val) 
                current.next = current.next.next;
            current = current.next;
        }
        return head;
    }
}

不加虚拟头节点

/**
 * 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) {
        ListNode virtual = new ListNode(-1, head);
        ListNode prew = virtual;
        ListNode current = head;
        while(current != null) {
            if(current.val == val) 
                prew.next = current.next;
            else prew = current;
            current = current.next;
        }
        return virtual.next;
    }
}

力扣19题-删除链表的倒数第N个节点

快慢指针

/**
 * 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 removeNthFromEnd(ListNode head, int n) {
        ListNode a = head;
        ListNode b = head;
        for(int i =0; i<n; i++){
            a = a.next;
        }
        // 删除头节点
        if(a == null)   return head.next;
        while(a.next != null){
            a = a.next;
            b = b.next;
        }
        b.next = b.next.next;
        return head;
    }
}

力扣24题-两两交换链表值

/**
 * 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 swapPairs(ListNode head) {
        ListNode fakeNode = new ListNode(0);
        fakeNode.next = head;
        ListNode pre = fakeNode;
        
        while(pre.next != null && pre.next.next != null){
            // 存储相邻两节点的后继节点
            ListNode temp = head.next.next;
            pre.next = head.next;
            head.next.next = head;
            head.next = temp;
            pre = head;
            head =  head.next; 
        }
        return fakeNode.next;
    }
}

力扣25题-K个一组翻转链表

每次循环K个节点,翻转后链接回去

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode hair = new ListNode(0);
        hair.next = head;
        ListNode pre = hair;

        while (head != null) {
            ListNode tail = pre;
            // 查看剩余部分长度是否大于等于 k
            for (int i = 0; i < k; ++i) {
                tail = tail.next;
                if (tail == null) {
                    return hair.next;
                }
            }
            ListNode nex = tail.next;
            ListNode[] reverse = myReverse(head, tail);
            head = reverse[0];
            tail = reverse[1];
            // 把子链表重新接回原链表
            pre.next = head;
            tail.next = nex;
            pre = tail;
            head = tail.next;
        }

        return hair.next;
    }

    public ListNode[] myReverse(ListNode head, ListNode tail) {
        ListNode prev = tail.next;
        ListNode p = head;
        while (prev != tail) {
            ListNode nex = p.next;
            p.next = prev;
            prev = p;
            p = nex;
        }
        return new ListNode[]{tail, head};
    }
}

力扣141题-环形链表

快慢指针,如果快指针反过来追上了慢指针,则存在环

public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode s = head, f = head;
        while(f != null && f.next != null){ 
            s = s.next;
            f = f.next.next;
            if(s == f) return true;
        }
        return false;
    }
}

142题-环形链表二

ab = cb,求出相遇节点后,设一个节点从head开始走,和slow相遇的节点即环的入口
在这里插入图片描述

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;

        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){
                ListNode flag = head;
                while(flag != slow){
                    flag = flag.next;
                    slow = slow.next;
                }
                return flag;
            }
        }
        return null;
    }
}
```# 总结


小小励志
>有些事你现在不做,一辈子都不会做了。
>如果你想做一件事,全世界都会为你让路。
《搭车去柏林》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值