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

206.反转链表

时间:2019-10-27 20:39:53      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:col   efi   def   span   while   init   color   思路   ==   

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.反转链表

标签:col   efi   def   span   while   init   color   思路   ==   

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

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