反转链表

题目:输入一个链表,反转链表后,输出新链表的表头。

 1 public class Solution {
 2     public ListNode ReverseList(ListNode head) {
 3          if(head==null){
 4              return null;
 5          }
 6          if(head.next==null){
 7              return head;
 8          }
 9 
10         ListNode pre=null;
11         ListNode cur=head;
12         ListNode  next=null;
13         while(cur!=null){
14              next=cur.next;
15              cur.next=pre;
16              pre=cur;
17              cur=next;
18              
19          }
20        return pre;
21     }
22 }

 

 

posted @ 2019-05-04 09:06  JingMo  阅读(102)  评论(0)    收藏  举报