LeetCode 206 - Reverse Linked List - 题解/Solution

https://leetcode.com/problems/reverse-linked-list/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Reverse a singly linked list.
*
* Example
*
* Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6
*
* Return: 6 --> 5 --> 4 --> 3 --> 6 --> 2 --> 1
*
* @author dongyuxi
*
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if (null == head || null == head.next) {
return head;
}
ListNode prev = head;
ListNode cur = head.next;
while (null != cur) {
ListNode next = cur.next;
cur.next = prev;
if (prev == head) {
prev.next = null;
}
prev = cur;
cur = next;
}
return prev;
}
}


支付宝 微信
文章目录