/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head == null) return null;
if(head.next == null) return head;
ListNode newHead = new ListNode(0);
ListNode curNode = head;
while(head != null){
curNode = head;
head = head.next;
curNode.next = newHead.next;
newHead.next = curNode;
}
return newHead.next;
}
}
反转链表
最新推荐文章于 2024-08-21 00:26:54 发布