24. 两两交换链表中的节点
题目详细:LeetCode.24
Java解法(递归):
class Solution {
public ListNode swapPairs(ListNode head) {
if(head == null || head.next == null)
return head;
ListNode next = head.next;
head.next = swapPairs(next.next);
next.next = head;
return next;
}
}
19.删除链表的倒数第N个节点
题目详细:LeetCode.19
Java解法(双指针(快慢指针)):
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode ans = new ListNode(-1, head), p_del = ans, p_last = ans;
while(0 < n--){
p_last = p_last.next;
最低0.47元/天 解锁文章
368

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



