一、翻转链表
/** * 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 reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } }// 递归 class Solution { public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { // null 或 单节点 return head; } ListNode p = reverseList(head.next); head.next.next = head; head.next = null; return p; } }