题目描述
输入一个链表,反转链表后,输出新链表的表头。
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 12 public ListNode ReverseList(ListNode head) { 13 ListNode lastNode = null, p = head, nextNode; 14 if (head == null) return null; 15 16 for (;p.next != null; p = nextNode) { 17 nextNode = p.next; 18 p.next = lastNode; 19 lastNode = p; 20 21 } 22 p.next = lastNode; 23 return p; 24 } 25 }