Leetcode 206: Reverse Linked List

Posted Keep walking

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 206: Reverse Linked List相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 206 Reverse Linked List 链表

LeetCode 206 Reverse Linked List

Leetcode 206: Reverse Linked List

[LeetCode]206. Reverse Linked List

206. Reverse Linked List(LeetCode)

Leetcode92. Reverse Linked List II && 206. Reverse Linked List