标签:
输入一个链表,反转链表后,输出链表的所有元素。
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode reverseHead = null;
ListNode node = head;
ListNode nodePre = null;
while (node != null) {
ListNode nodeNext = node.next;
if (nodeNext == null) {
reverseHead = node;
}
node.next = nodePre;
nodePre = node;
node = nodeNext;
}
return reverseHead;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5618025.html