[LeetCode]206. 反转链表(java实现)迭代
1. 题目
2. 读题(需要重点注意的东西)
思路(迭代法):
本题最主要的是判断while语句的条件
与最后返回的值
,如下动画所示,就会很明确:
3. 解法
---------------------------------------------------解法---------------------------------------------------
/**
* 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 pre = null;
ListNode cur = head;
while(cur != null){
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
可能存在的问题:
4. 可能有帮助的前置习题
5. 所用到的数据结构与算法思想
- 迭代法
- 指针
6. 总结
经典面试题,推荐在理解的基础上背下来!