文章目录 1. 题目2. 思路(1) 迭代(2) 递归 3. 代码 1. 题目 2. 思路 (1) 迭代 利用一个指针指向新链表的头结点,另一个指针指向需要转移的结点,遍历原链表将所有结点转移到新链表即可。 (2) 递归 当递归到最后一个结点时,直接返回该结点。利用下一个结点已经反转好的链表,令该链表的最后一个结点,即当前结点的下一个结点的next指向当前结点,然后使当前结点的next指向null,返回反转好的链表即可。 3. 代码 public class Test { public static void main(String[] args) { } } class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } class Solution { public ListNode reverseList(ListNode head) { ListNode newHead = null; ListNode temp = head; while (head != null) { head = head.next; temp.next = newHead; newHead = temp; temp = head; } return newHead; } } class Solution1 { public ListNode reverseList(ListNode head) { if (head == null) { return null; } if (head.next == null) { return head; } ListNode newHead = reverseList(head.next); head.next.next = head; head.next = null; return newHead; } }