码迷,mamicode.com
首页 > 其他好文 > 详细

反转链表

时间:2019-01-27 00:23:51      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:this   desc   nod   node   list   style   eve   val   null   

题目描述

输入一个链表,反转链表后,输出新链表的表头。
 
 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 }

 

反转链表

标签:this   desc   nod   node   list   style   eve   val   null   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!