1. 程式人生 > >Leetcode 206: Reverse Linked List

Leetcode 206: Reverse Linked List

blog while null pub ret nbsp leet public ever

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