LeetCode-探索-初级算法-链表-3. 反转链表(个人做题记录,不是习题讲解)
LeetCode探索-初级算法:https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/
- 反转链表
-
语言:java
-
思路:一开始没想好,后面看了下网上的思路,大致就是用多个指针,往后遍历的时候顺便就修改next了。用3个指针分别表示当前,上一个,下一个
-
代码(0ms):
/** * 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 pre = head,current = head,next = head; if(next == null) return null; next = next.next; pre.next = null; while(next!=null){ pre = current; current =