/**
* 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 head;
ListNode result = null ;
ListNode temp = null;
while (head!=null) {
if (head.next!=null)
{temp = head.next;
head.next = result;
result = head;
head = temp;}
else {
head.next = result;
result = head;
head = null;
}
}
return result;
}
}
206. Reverse Linked List
最新推荐文章于 2024-05-08 14:07:28 发布