代码随想录第四天|24.两两交换链表中的节点,19. 删除链表的倒数第 N 个结点,面试题 02.07. 链表相交,142. 环形链表 II

代码随想录第四天|24.两两交换链表中的节点,19. 删除链表的倒数第 N 个结点,面试题 02.07. 链表相交,142. 环形链表 II

24. 两两交换链表中的节点
class Solution {
    public ListNode swapPairs(ListNode head) {
        //建立虚拟节点
        ListNode newHead = new ListNode(0,head);
        ListNode pre = newHead;
        ListNode cur = head;
        ListNode next = null;
        while(cur != null && cur.next != null){
            next = cur.next.next;
            pre.next = cur.next;
            cur.next.next = cur;
            cur.next = next;
            pre = cur;
            cur = next;
        }
        return newHead.next;
    }
}

//递归法:

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null)return head;
        ListNode next = head.next;
        ListNode newNode = swapPairs(next.next);
        next.next = head;
        head.next = newNode;

        return next;
    }
}

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

//暴力法:先算出长度,用长度减去n,求出要跳的节点数
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newHead = new ListNode(0,head);
        ListNode temp = newHead;
        ListNode cur = newHead;
        int index = len(temp) - n - 1;
        while(index -- > 0){
            cur = cur.next;
        }
        cur.next = cur.next.next;
        return newHead.next;
    }
    int len(ListNode node){
        if(node == null)return 0;
        int count = 1;
        while(node.next != null){
            count ++;
            node = node.next;
        }
        return count;
    }
}
//双指针法:快指针跳n次,因为n代表倒数的节点,那么之后快慢指针同时跳,快指针跳到头,慢指针就跳到要删的节点前
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode newHead = new ListNode(0,head);
        ListNode fast = newHead;
        ListNode low = newHead;
        while(n-- > 0){
            fast = fast.next;
        }
        while(fast.next != null){
            low = low.next;
            fast = fast.next;
        }
        low.next = low.next.next;
        return newHead.next;
    }
}

面试题 02.07. 链表相交
//双指针法:当A指针跳到头,就往B链表跳,当B指针跳到头,就往A链表跳,
//如果链表相交,则会跳到同一个节点,如果不相交,则会跳到null
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
       ListNode nodeA = headA;
       ListNode nodeB = headB;
       while(nodeA != nodeB){
          
           nodeA = nodeA == null ? headB : nodeA.next;
           nodeB = nodeB == null ? headA : nodeB.next;
           
       } 
       return nodeA;
    }
}


142. 环形链表 II
//一开始自己的思路,大体是对的,被各种细枝末节的条件困了好久,才一点点改出来这个解题思路,因为是自己好不容易做出来的,虽然很拉,但我还是要贴出来
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode cur = head;
        ListNode pre = head;
        while(cur != null &&cur.next != null ){
            cur= cur.next.next;
            pre = pre.next;
            if(cur == pre)break;
        }
        if(cur == null ||cur.next == null)return null;
        pre = head;
        while( cur != pre){
            cur = cur.next;
            pre= pre.next;
            
        }
        return pre;
    }
}
//卡哥的思路就很清晰
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个

红包金额最低5元

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

抵扣说明:

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

余额充值