单链表翻转,固定4步,三个指针

反转链表

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

初始化 pre=null,cur=head;

  1. next=cur.next;//保存后继节点。

  2. cur.next=pre;//翻转或逆序

  3. pre=cur;//后移

  4. cur=next;// 后移

     /**
      * 输入一个链表,反转链表后,输出新链表的表头。
      * @param head 链表
      * @return 反转链表后的表头
      */
     public ListNode ReverseList(ListNode head) {
         if(head==null||head.next==null){
             return head;
         }
         ListNode pre=null;
         ListNode cur=head;
         while (cur!=null){
             ListNode next= cur.next;
             cur.next=pre;
             pre=cur;
             cur=next;
         }
         return pre;
    
     }
    
全部评论

相关推荐

02-28 01:18
已编辑
南昌大学 后端工程师
黑皮白袜臭脚体育生:把开源经历放个人项目上边应该更好,就像大部分人都把实习经历放个人项目上边
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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