题意

单链表反转

题目来源:https://leetcode.com/problems/reverse-linked-list/

标记难度:Easy

提交次数:1/?

代码效率:100%

分析

迭代法:将当前节点的后继节点指向前一个节点,移动当前节点

递归法

代码

迭代法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode newHead = null;
while(head != null){
ListNode next = head.next;
head.next = newHead;
newHead = head;
head = next;
}
return newHead;
}
}

递归法

1
2


Nonesense

最简单的单链表居然花费了我半天的时间,最后还是参考别人的答案才写出来。。。智商捉急

总结一下问题:

  • 太急躁,要静下心来慢慢梳理过程,整理成代码输出
  • java变量指针等概念感觉还是不清晰,今天抽空需要补一下