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

Leetcode 206: Reverse Linked List

时间:2018-01-31 14:34:37      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:ini   ret   ==   for   head   gpo   body   ever   style   

Reverse a singly linked list.

 

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

 

Leetcode 206: Reverse Linked List

标签:ini   ret   ==   for   head   gpo   body   ever   style   

原文地址:https://www.cnblogs.com/liangmou/p/8391038.html

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