算法学习Day3——链表专项2

19 删除链表的倒数第N个节点

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

两个指针,一个指向第一个节点,另外一个指向第n个节点,然后一直往后移动.

注意:快慢指针要指向虚拟头结点,否则如果长度为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 dummyNode = new ListNode(0);
        dummyNode.next=head;
        ListNode firstNode=dummyNode;
        ListNode secondNode=dummyNode;
        for (int i = 0; i <= n  ; i++){
            secondNode=secondNode.next;
        }
        while(secondNode!=null){
            firstNode=firstNode.next;
            secondNode=secondNode.next;
        }
        firstNode.next=firstNode.next.next;
        return dummyNode.next;

    }
}

面试题 02.07 链表相交

面试题 02.07. 链表相交 - 力扣(LeetCode)

求出两个链表长度,并且求出两个链表长度的差值,让两个链表末尾对齐

注意:让curA为最长链表的头,lenA为其长度,如果不是就交换一下

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA=headA;
        ListNode curB=headB;
        int lenA=0,lenB=0;
        while(curA!=null){
            lenA++;
            curA=curA.next;
        }
        while(curB!=null){
            lenB++;
            curB=curB.next;
        }
        curA=headA;
        curB=headB;
        //让curA为最长链表的头,lenA为其长度,如果不是就交换一下
        if(lenB>lenA){
            int tmplen=lenA;
            lenA=lenB;
            lenB=tmplen;
            ListNode tmpNode=curA;
            curA=curB;
            curB=tmpNode;
        }
        int gap=lenA-lenB;
        while(gap-->0){
            curA=curA.next;
        }
        while(curA!=null){
            if(curA==curB){
                return curA;
            }
            curA=curA.next;
            curB=curB.next;
        }
        return null;
        
    }
}

142 环形链表II

142. Linked List Cycle II - 力扣(LeetCode)

使用快慢指针。

关键点有两个:1. 判断链表是否有环。2. 找到环的入口。(使用数学公式进行推算)

当n等于1的时候,x=z,意味着,从头结点和相遇位置同时出发一个指针,两个指针的相遇位置就是环形入口。

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 index1=fast;
                ListNode index2=head;
                while(index1!=index2){
                    index1=index1.next;
                    index2=index2.next;
                    
                }
                return index1;
            }
        }
        return null;
        
    }
}

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

endless_?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值