/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if (head == null) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode current = head;
while (current.next != null) {
current = current.next;
}
ListNode tail = current;
current = dummy;
while (dummy.next != tail) {
current = dummy.next;
dummy.next = current.next;
current.next = tail.next;
tail.next = current;
}
return dummy.next;
}
}
Reverse Linked List
最新推荐文章于 2021-11-19 09:03:47 发布