跳至主要內容

206. 反转链表

T4mako算法链表递归小于 1 分钟

206. 反转链表

简单

题目描述open in new window

解法思路:

  • 定义两个指针,初始指向 null 和 head
  • 在两个指针不断后移吧并改变指针的指向,时间复杂度为O(n)
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode p = null;
        ListNode n = head;
        while(n != null){
            ListNode temp = n;
            n = n.next;
            temp.next = p;
            p = temp;
        }
        return p;
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5