题目:
输入一个链表,反转链表后,输出新链表的表头
解答:
1 public class Solution { 2 3 public static ListNode reverseList(ListNode pHead) { 4 if(pHead == null) { 5 return null; 6 } 7 8 ListNode pReversedHead = null; 9 10 ListNode pPrev = null; 11 ListNode pCurrent = pHead; 12 ListNode pNext = null; 13 14 while(current != null) { 15 pNext = pCurrent.next; 16 17 if(pNext == null) { 18 pReversedHead = pCurrent; 19 } 20 21 pCurrent.next = pPrev; 22 23 pPrev = pCurrent; 24 pCurrent = pNext; 25 } 26 27 return pReversedHead; 28 } 29 }