首页 > 其他 > 详细

【链表】反转链表

时间:2016-09-17 10:35:59      阅读:148      评论:0      收藏:0      [点我收藏+]

 

输入一个链表,反转链表后,输出链表的所有元素。

 1 /*
 2 public class ListNode {
 3     int val;
 4     ListNode next = null;
 5 
 6     ListNode(int val) {
 7         this.val = val;
 8     }
 9 }*/
10 public class Solution {
11     public ListNode ReverseList(ListNode head) {
12 
13         if (null == head || null == head.next) {
14             return head;
15         }
16 
17         ListNode lastNode = head;
18         ListNode currNode = head.next;
19         ListNode preNode = head.next.next;
20 
21         while(null != preNode){
22             currNode.next = lastNode;
23 
24             if (lastNode == head) {
25                 lastNode.next = null;
26             }
27 
28             lastNode = currNode;
29             currNode = preNode;
30             preNode = preNode.next;
31         } 
32         
33         currNode.next = lastNode;
34 
35         return currNode;
36     
37     }
38 }

 

【链表】反转链表

原文:http://www.cnblogs.com/jiangyi-uestc/p/5878049.html

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