LeetCode: Reverse Linked List

C#

 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) return null;

12         ListNode p = head, pPre = null;

13         while (p != null) {

14             ListNode pNext = p.next;

15             p.next = pPre;

16             pPre = p;

17             p = pNext;

18         }

19         return pPre;

20     }

21 }
View Code

 

你可能感兴趣的:(LeetCode)