【题目描述】
输入一个链表,反转链表后,输出新链表的表头。
【AC代码】
Reference: https://blog.nowcoder.net/n/2b3b3f502ee84080a1874f1d3ebb896e?f=comment
1 /* 2 public class ListNode { 3 int val; 4 ListNode next = null; 5 6 ListNode(int val) { 7 this.val = val; 8 } 9 }*/ 10 public class Solution { 11 public ListNode ReverseList(ListNode head) { 12 if (head == null || head.next == null) return head; 13 ListNode pre = null; 14 ListNode next = null; 15 while (head != null) { 16 next = head.next; 17 head.next = pre; 18 pre = head; 19 head = next; 20 } 21 return pre; 22 } 23 }
原文:https://www.cnblogs.com/moongazer/p/11628325.html