首页 > 其他 > 详细

206.反转链表

时间:2019-10-27 21:13:47      阅读:85      评论:0      收藏:0      [点我收藏+]

1.题目描述:

  反转一个单链表。

2.解题思路及代码:

  迭代法

    

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public ListNode reverseList(ListNode head) {
11         if(head==null)
12             return null;
13         ListNode pre=null;
14         ListNode cur=head;
15         ListNode tmp=null;
16         while(cur.next!=null){
17             tmp=cur.next;
18             cur.next=pre;
19             pre=cur;
20             cur=tmp;
21         }
22         cur.next=pre;
23         return cur;
24     }
25 }

 

    

206.反转链表

原文:https://www.cnblogs.com/teensSpirit-code-life/p/11748638.html

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