题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
//新建一个链表
ListNode newHead=null;
while(head!=null){
//存储原链表的下一结点
ListNode temp=head.next;
//将原链表头结点采用头插法插入新链表
head.next=newHead;
//更新新链表表头
newHead=head;
//更新原链表表头
head=temp;
}
return newHead;
}
}