数据结构与算法(三)单链表反转---腾讯面试题

接上面的单向链表实现,单链表的反转。

 /**
     *  单链表反转
     * @param headNode
     */
    public  void reverseList(Node headNode){
        if (headNode.getNext() == null || headNode.getNext().getNext() == null){
            return;
        }
        Node cur=headNode.getNext();
        Node next=null;
        Node reverseHeadNode=new Node(0,"");
        while (cur!=null){
            next=cur.getNext();
            cur.setNext(reverseHeadNode.getNext());
            reverseHeadNode.setNext(cur);
            cur=next;
        }
        headNode.setNext(reverseHeadNode.getNext());
    }
# 数据结构  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×