Leetcode--Java--206. Reverse Linked List

本文详细介绍了如何使用迭代和递归两种方法来反转单链表。迭代法采用双指针,逐步反转节点的指向;递归法则通过不断划分子问题,反转链表的后续部分。这两种方法都有效地实现了链表的反转操作,适用于数据结构和算法的学习与实践。

题目描述

Given the head of a singly linked list, reverse the list, and return the reversed list.
Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?

样例描述

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,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; }
 * }
 */

思路

  1. 迭代法(双指针法)
    在这里插入图片描述
    维护两个相邻的指针同时往后遍历,每次反转右边指针的指向,让其指向左边的指针,直到右边的指针走到空,最后记得将第一个结点的指针指向空!
  2. 递归法:将接口视为黑箱操作(每次可以反转以head为头结点的,并返回尾结点),不断划分子问题
    在这里插入图片描述
    然后将head.next结点的指针指向headhead指向空,返回尾结点就行
    在这里插入图片描述

代码

  1. 迭代之双指针法
class Solution {
    public ListNode reverseList(ListNode head) {
      if (head == null) return head;
      ListNode p = head, q = head.next;
       while (q != null){
           //提前存下q的后面结点,因为要反转q的指向
           ListNode  r = q.next;
           //反转
           q.next = p;
           //两个指针同时向后走
           p = q;
           q = r;
       }
       head.next = null;
       //返回第一个指针,因为第二个指向空,第一个指向尾结点,也就是逆序后的头结点
       return p;
    }
}
  1. 递归法
class Solution {
    public ListNode reverseList(ListNode head) {
        //空或者只有一个结点 
        if (head == null || head.next == null) return head;
        //反转以head.next为头结点的链表,返回尾结点
        ListNode tail = reverseList(head.next);
        //head.next为头结点的链表后面的指针都反转了,所以反转head.next的指针
        head.next.next = head;
        head.next = null;
       //返回前面得得尾结点
        return tail;
    }
}
以下是几种 LeetCode 234 题回文链表问题的 Python 实现: ### 方一:将链表复制到数组里再从两头比对 ```python # Definition for singly-linked list. class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def isPalindrome(self, head: ListNode) -> bool: lst = [] node = head while node: lst.append(node.val) node = node.next start = 0 end = len(lst) - 1 while start < end: if lst[start] != lst[end]: return False start += 1 end -= 1 return True ``` ### 方二:递归 ```python # Definition for singly-linked list. class ListNode(object): def __init__(self, val=0, next=None): self.val = val self.next = next class Solution(object): def isPalindrome(self, head): front_pointer = head def recursively_check(current_node=head): if current_node is not None: if not recursively_check(current_node.next): return False if front_pointer.val != current_node.val: return False nonlocal front_pointer front_pointer = front_pointer.next return True return recursively_check() ``` ### 方三:快慢指针 + 反转链表 ```python # Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: def isPalindrome(self, head: ListNode) -> bool: if head is None: return True first_half_end = self.end_of_first_half(head) second_half_start = self.reverse_list(first_half_end.next) result = True first_position = head second_position = second_half_start while result and second_position is not None: if first_position.val != second_position.val: result = False first_position = first_position.next second_position = second_position.next first_half_end.next = self.reverse_list(second_half_start) return result def end_of_first_half(self, head): fast = head slow = head while fast.next is not None and fast.next.next is not None: fast = fast.next.next slow = slow.next return slow def reverse_list(self, head): previous = None current = head while current is not None: next_node = current.next current.next = previous previous = current current = next_node return previous ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值