LeetCode_206 发表于 2020-10-24 | 分类于 DataStructure | Leetcode 206反转一个单链表。123456789public class ListNode{ int val; ListNode next; public ListNode(int val) { this.val = val; }} 12输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL 遍历法12345678910111213141516171819202122class Solution{ public ListNode reverseList(ListNode head) { if(head == null || head.next == null) { return head; } ListNode pre = null; ListNode cur = head; while(cur != null) { ListNode temp = cur.next; cur.next = pre; pre = cur; cur = temp; } return pre; }} 递归法123456789101112131415class Solutin{ public ListNode reverseList(ListNode head) { if(head == null || head.next == null) { return head; } ListNode newNode = reverseList(head.next); head.next.next = head; head.next = null; return newNode; }} Donate comment here 打赏 微信支付 本文作者: NoTrouble 本文链接: http://yoursite.com/2020/10/24/LeetCode-206/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!