Reverse a singly linked list.
这道题非常基础,需要熟练掌握
/**
* 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) {
ListNode pre = null;
while(head != null){
ListNode node = new ListNode(head.val);
node.next = pre;
pre = node;
head = head.next;
}
return pre;
}
}
本站原创文章皆遵循“署名-非商业性使用-相同方式共享 3.0 (CC BY-NC-SA 3.0)”。转载请保留以下标注: