代码随想录算法训练营Day4|LC24两两交换链表中的节点&LC19删除链表的倒数第N个结点&LC160相交链表&LC142环形链表II

今天的题感觉也都是链表基础水平。总体而言不难。

原题链接: 24 两两交换链表中的节点

没什么好说的,上来设置一个虚拟头结点狠狠迭代了。看题解还有一种递归的写法,实在想不到。

迭代写法:

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

递归写法(官方解法): 

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

原题链接:19 删除链表的倒数第N个结点

这题也是一顿迭代结束。先遍历一遍找到结点总数,然后再次遍历到要删除结点的前一个结点,修改其指针即可。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        int len = 0;
        ListNode tmp = head;
        while (tmp != null) {
            len++;
            tmp = tmp.next;
        }
        ListNode cur = dummy;
        for (int i = 1; i < len - n + 1; i++) {
            cur = cur.next;
        }
        cur.next = cur.next.next;
        ListNode ans = dummy.next;
        return ans;
    }
}

另外官解的栈方法稍有不同,一并附上。本质上也是两趟遍历。

class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode dummy = new ListNode(0, head);
        Deque<ListNode> stack = new LinkedList<>();
        ListNode cur = dummy;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        for (int i = 0; i < n; ++i) {
            stack.pop();
        }
        ListNode pre = stack.peek();
        pre.next = pre.next.next;
        return dummy.next;
    }
}

原题链接: 160 相交链表

 最简单的做法无过于用一个哈希集合存储链表A的每个结点,然后遍历链表B的每个结点,如果已经存在,那么这个交点就是这个已存在的结点。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        Set<ListNode> set = new HashSet<>();
        ListNode cur = headA;
        while (cur != null) {
            set.add(cur);
            cur = cur.next;
        }
        cur = headB;
        while (cur != null) {
            if (set.contains(cur)) return cur;
            cur = cur.next;
        }
        return null;
    }
}

官解的双指针做法非常有启发。

如果两链表相交,且不相交部分长度相等情况下,两指针在第一轮遍历时就会指向同一结点并将其返回;而在相交部分长度不相等的情况下,两指针会在完整遍历完一次链表后同时到达链表相交点并将其返回。

如果两链表不相交,且链表长度相等时,会出现完整遍历一次链表后pA == pB == null,即两指针同时指向null的情况,那么返回的就是null;链表长度不相等时,两指针就都会完整遍历完两个链表,最后出现的也是pA == pB == null,返回的还是null。

public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }
}

原题链接:142 环形链表

 首先容易想到的也是一次遍历+哈希集合的方法。执行用时较长。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode pos = head;
        Set<ListNode> set = new HashSet<>();
        while (pos != null) {
            if (set.contains(pos)) return pos;
            else {
                set.add(pos);
                pos = pos.next;
            }
        }
        return null;
    }
}

 然后就是双指针解法,首先利用快慢指针先来判断链表中是否存在环。慢指针每次移动到后面一个结点,快指针移动到后面第二个结点,如果两指针相遇,那么说明有环。在存在环时,另外设置一个指针从head开始,与slow指针同时逐一遍历,相遇时所在的结点即链表中环的第一个结点。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode slow = head, fast = head;
        while (fast != null) {
            slow = slow.next;
            if (fast.next != null) fast = fast.next.next;
            else return null;
            if (fast == slow) {
                ListNode cur = head;
                while (cur != slow) {
                    cur = cur.next;
                    slow = slow.next;
                }
                return cur;
            }
        }
        return null;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值