首页 > 其他 > 详细

复杂度O(n)倒转链表

时间:2015-09-11 01:24:55      阅读:229      评论:0      收藏:0      [点我收藏+]
 1 public class ListNode {
 2      int val;
 3      ListNode next;
 4      ListNode(int x) { val = x; }
 5      ListNode(){}
 6 
 7       public static ListNode revese(ListNode input)
 8     {
 9         ListNode head = new ListNode();//头插法的头
10         ListNode cur = input;                 //指向当前位置
11         ListNode headnext , next;          //两个龙套用于记下next 
12         while (cur != null)
13         {
14             headnext = head.next;   //龙套1登场
15                 next = cur.next;             //龙套2登场
16             head.next = cur;            //cur插到头后边
17             cur.next = headnext;      //连接龙套1
18             cur = next;                    //龙套2变成当前位置
19         }
20         return head.next;                  
21     }
22 }

 

复杂度O(n)倒转链表

原文:http://www.cnblogs.com/guizhongyi/p/4799712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!