标签:
输入一个链表,反转链表后,输出链表的所有元素。
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 13 if (null == head || null == head.next) { 14 return head; 15 } 16 17 ListNode lastNode = head; 18 ListNode currNode = head.next; 19 ListNode preNode = head.next.next; 20 21 while(null != preNode){ 22 currNode.next = lastNode; 23 24 if (lastNode == head) { 25 lastNode.next = null; 26 } 27 28 lastNode = currNode; 29 currNode = preNode; 30 preNode = preNode.next; 31 } 32 33 currNode.next = lastNode; 34 35 return currNode; 36 37 } 38 }
标签:
原文地址:http://www.cnblogs.com/jiangyi-uestc/p/5878049.html