题解 | #反转链表#

反转链表

http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

头插法倒序

public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode newhead=new ListNode(-1);
        ListNode after=new ListNode(-1);
        while(head!=null){
            after=head.next;//原来链表后续节点

            ListNode node=newhead.next;//新链表头插
            newhead.next=head;//头插
            head.next=node;//头插

            head=after;          
        }
        return newhead.next;
    }
}
全部评论

相关推荐

09-19 12:15
门头沟学院 Java
迷茫的大四🐶:这下是真的打牌了,我可以用感谢信和佬一起打牌吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务