剑指Offer-Java-反转链表

Posted 水坚石青

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了剑指Offer-Java-反转链表相关的知识,希望对你有一定的参考价值。

反转链表


题目:
输入一个链表,反转链表后,输出新链表的表头。
代码:

package com.hlq.test;

/**
 * @author helongqiang
 * @date 2020/5/17 12:38
 */

/**
 * 输入一个链表,反转链表后,输出新链表的表头。
 */

public class Solution {

    public ListNode ReverseList(ListNode head){
        if(head == null){
            return null;
        }
        ListNode pre = null;
        ListNode nex = null;
        while(head.next != null){
            nex = head.next;
            head.next = pre;
            pre = head;
            head = nex;
        }
        head.next = pre;
        return head;
    }
}

以上是关于剑指Offer-Java-反转链表的主要内容,如果未能解决你的问题,请参考以下文章

剑指Offer-Java-链表中倒数第k个结点

算法---- Leetcode剑指offer-Java版题解

算法---- Leetcode剑指offer-Java版题解

剑指offer反转链表python

《剑指Offer——24. 反转链表》代码

剑指Offer-反转链表