代码随想录一刷day04

代码随想录一刷04day

文章目录

  • 前言
  • 一、力扣24两两交换链表中的节点
    • 1.迭代写法
    • 1.递归写法
  • 二、力扣 19删除链表的倒数第N个节点
    • 1.多遍扫描(简单)
    • 1.一遍扫描(进阶)双指针的经典应用
  • 三、力扣 面试题 02.07. 链表相交
  • 四、力扣 142.环形链表II
  • 总结


前言

链表的循环要找准结束条件和最小循环结构,先分析清楚往往事半功倍


提示:以下是本篇文章正文内容,下面案例可供参考

一、力扣24两两交换链表中的节点

要找到最小的循环结构,保证循环可以跑起来

1.迭代写法

/**
 * 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 dumyHead = new ListNode(-1, head);
        ListNode cur = dumyHead;
        ListNode firstNode;
        ListNode secondNode;
        while(cur.next!=null&&cur.next.next!=null){
            ListNode temp = cur.next.next.next;
            firstNode = cur.next;
            secondNode = firstNode.next;
            cur.next = secondNode;
            secondNode.next = firstNode;
            firstNode.next = temp;
            cur = firstNode;
        }
        return dumyHead.next;
    }
}

1.递归写法

/**
 * 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) {
        if(head==null||head.next==null)return head;
        ListNode firstNode = head.next;
        ListNode newNode = swapPairs(firstNode.next);
        firstNode.next = head;
        head.next = newNode;
        return firstNode;
    }
}

二、力扣 19删除链表的倒数第N个节点

1.多遍扫描(简单)

/**
 * 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) {
        int length = 0;
        ListNode dumyNode = new ListNode(-1, head);
        ListNode p = dumyNode.next;
        while(p!=null){
            length++;
            p = p.next;
        }
        if(n>length)return head;
        int gap = length - n;
        int count = 0;
        p = dumyNode;
        while(count<gap){
            p = p.next;count++;
        }
        p.next = p.next.next;
        return dumyNode.next;
    }
}

1.一遍扫描(进阶)双指针的经典应用

设置快慢指针

/**
 * 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 dumyNode = new ListNode(-1, head);
        ListNode fast = dumyNode;
        ListNode slow = dumyNode;
        int count = 0;
        while(fast!=null){
            if(count<n){
                count++;fast= fast.next;
            }else{
                if(fast.next!=null){
                    fast = fast.next;
                    slow = slow.next;
                }else{
                    slow.next = slow.next.next;
                    return dumyNode.next;
                }
            }
        }
        return dumyNode.next;
    }
}

三、力扣 面试题 02.07. 链表相交

链表相交指的是,节点的地址相同,先找到两条链表等长的位置开始遍历,比较两两是否地址相等,即可找到交点

/**
 * 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) {
        int lA = 0, lB = 0, temp = 0;
        ListNode longNode, shortNode;
        ListNode dumyA = new ListNode(-1); dumyA.next = headA;
        ListNode dumyB = new ListNode(-1); dumyB.next = headB;
        ListNode pA = dumyA;
        ListNode pB = dumyB;
        while(pA.next!=null){lA++; pA = pA.next;}
        while(pB.next!=null){lB++; pB = pB.next;}
        if(lA>=lB){
            temp = lA - lB;
            longNode = dumyA;
            shortNode = dumyB;
        }else{
            temp = lB - lA;
            longNode = dumyB;
            shortNode = dumyA;
        }
        while(temp>0){temp--; longNode = longNode.next;}
        while(longNode!=null&&shortNode!=null){
            if(longNode==shortNode){
                return shortNode;
            }else{
                longNode = longNode.next;
                shortNode = shortNode.next;
            }
        }
        return null;
    }
}

四、力扣 142.环形链表II

采用快慢指针,快指针一次走两个节点,慢指针一次走一个节点,可以保证慢指针不会被快指针跳过

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode fastNode = head;
        ListNode slowNode = head;
        while(fastNode!=null&&fastNode.next!=null){
            fastNode = fastNode.next.next;
            slowNode = slowNode.next;
            if(fastNode==slowNode){
                ListNode index1 = fastNode;
                ListNode index2 = head;
                while(index1!=index2){
                    index1 = index1.next;
                    index2 = index2.next;
                }
                return index1;
            }
        }
        return null;
    }
}

总结

反复练习,可以更加熟练的掌握

你可能感兴趣的:(链表,leetcode,算法)