csdn这个推广我随手点的,很多机器人点赞关注我哈哈哈,有活人吗?感觉像在一个空旷废弃的学校里写日记~~
这个不难,我是新建了一个链表,复制了每一个结点再连接起来。等会儿试试在原基础链表上修改是什么情况。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode head1 = new ListNode(-1,null);//新链表的头
ListNode cur = head;
while(cur!=null){
ListNode now = new ListNode(cur.val);//复制每个结点
now.next = head1.next;
head1.next = now;
cur = cur.next;
}
return head1.next;
}
}