题目

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

例如:

问题分析

反转链表,首先想到的是让最后一个结点作为头结点,第一个结点作为尾结点指向 null。

如果我们把指针反方向改变,就可以轻松完成整个链表的反转。

说到改变指针,我们有两个方式,第一种是从前到后改变指针,第二种是从后往前改变指针。从前往后代表迭代方式,从后往前是递归的方式,这个不难理解,直接上动画。

动画实现

递归法

迭代法

解题思路

迭代法

从前到后开始反转,我们需要三个指针,第一个指针指向当前头结点 head,第二个指针指向第二个节点 curP(当前结点),第三个指针为保存下一个节点的临时节点 temp。

1、反转顺序为,先保存下一节点。

2、然后让当前结点的指向前一节点。

3、最后移动当前结点到下一结点。(head 头节点一开始初始化指向 null)。

4、重复该循环。

递归法

递归法只不过换了一个顺序,先递进去,然后在执行以上反转操作。

1、让当前结点的下一节点的下一节点指向当前结点。

2、当前节点的下一结点指向 null,直到递归完毕,完成反转。

代码实现

javaScript

//& 反转链表 - 递归实现var reverseList = function(curP) {  if(curP == null || curP.next == null) return curP;  var tail = reverseList(curP.next)  curP.next.next = curP  curP.next = null  return tail;};//& 反转链表 — 迭代实现var reverseList = function(head) {  if(head == null || head.next == null) return head;  var curP = head.next  head.next = null  var temp = null  while(curP !== null){    temp = curP.next    curP.next = head    head = curP    curP = temp  }  return head;} 

java

//& 反转链表 - 迭代实现class Solution {    public ListNode reverseList(ListNode head) {        if(head == null) return null;        ListNode pre = null;        ListNode curP= head;        while(curP != null){            ListNode temp = curP.next;            curP.next = pre;            pre = curP;            curP = temp;        }        return  pre;    }}//& 反转链表 — 递归实现class Solution {    public ListNode reverseList(ListNode head) {        if(head == null || head.next == null) return head;        ListNode tail = reverseList(head.next);        head.next.next = head;        head.next = null;        return tail;    }}

python

//& 反转链表 - 迭代实现class Solution:    def ReverseList(self, pHead):        if not pHead or not pHead.next:            return pHead        last = None         while pHead:            tmp = pHead.next               pHead.next = last            last = pHead            pHead = tmp        return last//& 反转链表 — 递归实现class Solution:    def reverseList(self, head: ListNode) -> ListNode:        if not head:            return None        if not head.next:            return head        headNode = self.reverseList(head.next)        head.next.next = head        head.next = None        return headNode

测试用例

  • 输入空的头结点(null)

  • 输入一个节点的链表

  • 输入多个节点的链表
©著作权归作者所有:来自51CTO博客作者mb5fe1601ede528的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. PHP使用递归按层级查找数据(代码详解)
  2. PHP递归算法的应用(含示例)
  3. 什么是php递归
  4. php递归经典案例
  5. PHP中的递归是什么?实现方式有哪些?
  6. 递归就这么简单

随机推荐

  1. 【Android开发】背景选择器selector用法
  2. 阅读手札:《:第一行代码》(第一章)
  3. 基于Android FrameLayout的使用详解
  4. 使用ProgressBar实现加载进度条
  5. Android的安全体系
  6. Android 软键盘
  7. Android Display System --- Surface Fli
  8. android MediaPlayer详解
  9. Android基础UI篇------TextView及其子类
  10. [整理]学习Android的博客和网站