题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
保存当前节点的下一个节点,之后将当前节点的next指针指向前一个节点
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode newHaed = null;
ListNode temp = head;
while(head!=null){
temp = head.next;
head.next = newHaed;
newHaed = head;
head = temp;
}
return newHaed;
}
}