首页 > 其他 > 详细

反转链表

时间:2019-01-27 00:15:01      阅读:185      评论: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         if(head==null)return null;
13         ListNode cur = head;
14         ListNode pre = null;
15         ListNode temp = null;
16         while(cur.next!=null){
17             pre=cur;
18             cur=cur.next;
19             pre.next=temp;
20             temp=pre;
21         }
22         cur.next=pre;
23         return cur;
24     }
25 }

 

反转链表

原文:https://www.cnblogs.com/yihangZhou/p/10325287.html

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