题解 | #反转链表#新思路
反转链表
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; } }