题解 | #反转链表#新思路

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

import java.util.*;

public class Solution {
    public ListNode ReverseList (ListNode head) {
        // write code here
        ListNode res = new ListNode(-1);//头结点前面
        res.next = head;
        ListNode pre = res;
        if(head == null){
            return head;
        }

        // ListNode first = head;

        while(head.next != null){
            //每次都把后面的节点插入到反转部分的最前面去
            ListNode temp = head.next;
            head.next = temp.next; //first向后移动一个结点
            temp.next = pre.next; // 连接前面的结点
            pre.next = temp;
        }
        return res.next;
    }
}

全部评论

相关推荐

爱读书的放鸽子能手很...:刷个两端实习,冲春招,流水线什么时候不能去
我的秋招日记
点赞 评论 收藏
分享
野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务