04-链表-24. 两两交换链表中的节点19.删除链表的倒数第N个节点面试题 02.07. 链表相交142.环形链表II

24两两交换

采用虚拟头节点避免考虑开头的问题。搞清楚需要定义几个变量,考虑哪几个变量。

class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode cur = dummyHead, next = head;
        while(cur.next != null && cur.next.next != null){
            cur.next = next.next;
            next.next = cur.next.next;
            cur.next.next = next;
            
            cur = cur.next.next;
            next = cur.next;
        }
        return dummyHead.next;

    }
}代码片

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

思路十分巧妙:快慢指针,让快指针先走N+1步,慢指针跟他一起走,直到快指针指向null,慢指针正好指向前一个位置

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode slow = dummyHead,fast = dummyHead;
        for(int i = 0; i < n+1; i ++){
            fast = fast.next;
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummyHead.next;
    }
}

面试02.07链表相交

思路:遍历算出长度,尾端对齐,挨个对比是否相等。
自己写的时候没有注意到,遍历算出长度后要把指针放回去。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode curA = headA,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;
        if(lenA < lenB){
            ListNode tNode = curA;
            curA = curB;
            curB = tNode;
            int temp = lenA;
            lenA = lenB;
            lenB = temp;
        }
        int l = lenA -lenB;
        while(l-- > 0){
            curA = curA.next;
        }
        while(curA != null){
            if(curA == curB){
                return curA;
            }
            curA = curA.next;
            curB = curB.next;
        }
        return null;
    }
}

142环形链表2

给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

为了表示给定链表中的环,使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。

思路:看代码随想录视频,设计快慢指针。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode slow = head,fast = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                ListNode index1 = head;
                while(index1 != slow){
                    index1 = index1.next;
                    slow = slow.next;
                }
                return index1;
            }
        }
        return null;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值