首页 > 其他 > 详细

【剑指OFFER】反转链表

时间:2019-10-06 21:01:02      阅读:39      评论:0      收藏:0      [点我收藏+]

【题目描述】

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

 

【AC代码】

Reference: https://blog.nowcoder.net/n/2b3b3f502ee84080a1874f1d3ebb896e?f=comment

技术分享图片
 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 || head.next == null) return head;
13         ListNode pre = null;
14         ListNode next = null;
15         while (head != null) {
16             next = head.next;
17             head.next = pre;
18             pre = head;
19             head = next;
20         }
21         return pre;
22     }
23 }
View Code

 

【剑指OFFER】反转链表

原文:https://www.cnblogs.com/moongazer/p/11628325.html

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