24.两两交换链表中的节点
题目链接与讲解:代码随想录
思路:
代码实现:
/**
* 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) {
// base case 退出提交
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个节点
题目与讲解:
代码实现:
思想:
面试题02.07.链表相交
题目与讲解:
142.环形链表
题目与链接:
思想:
代码实现:
本文介绍了如何在链表中进行节点的两两交换,删除倒数第n个节点的操作,以及解决链表相交和环形链表的问题。这些是常见的数据结构题目,主要涉及链表的遍历、修改和递归算法的应用。
283

被折叠的 条评论
为什么被折叠?



