1. 题目描述
输入一个链表,反转链表后,输出新链表的表头。
原题传送门:link.
2. 提取关键词
反转链表
3. 思路
- 头插法
- 返回头节点应该为newhead.next
- newhead应该被定义为链表
- 递归方法传送门:link.
4. 代码实现(Java)
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode newhead= new ListNode(-1);
newhead.next=null;
while(head!=null){
ListNode next=head.next;
head.next=newhead.next;
newhead.next=head;
head=next;
}
return newhead.next;
}
}
5. 知识积累
暂无