博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
输入一个链表,反转链表后,输出新链表的表头。
阅读量:725 次
发布时间:2019-03-21

本文共 579 字,大约阅读时间需要 1 分钟。

题目

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

示例1

  • 输入

{1,2,3}

  • 返回值

{3,2,1}

说明:本题目包含复杂数据结构ListNode

Java

/*public class ListNode {    int val;    ListNode next = null;    ListNode(int val) {        this.val = val;    }}*/public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return null; ListNode pre = null; ListNode next = null; while(head != null) {
next = head.next; //断开下结点与当前结点的连接, 与前一结点连接 head.next = pre; pre = head; head = next; } return pre; }}

在这里插入图片描述

转载地址:http://snpgz.baihongyu.com/

你可能感兴趣的文章