// 迭代法,时间复杂度o1,空间复杂度on
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
// 时间复杂度on 空间复杂度on 递归深度达到 on
public ListNode reverseList(ListNode head) {
return reverse(head, null);
}
private ListNode reverse(ListNode cur, ListNode pre) {
if (cur null) return pre;
ListNode res = reverse(cur.next, cur);
cur.next = pre;
return res;
}
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None or head.next is None:
return head
newhead = self.reverseList(head.next)
head.next.next = head
head.next = None
return newhead
迭代(原地反转)
时间复杂度O(n),空间复杂度O(1)
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
prev = None
curr = head
while curr:
next_curr = curr.next
next_prev = curr # don't use prev.next
curr.next = prev
curr = next_curr
prev = next_prev
return prev
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null, cur = head, next = null;
while(cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#dumpy=ListNode(None)
dumpy = None
if head None:
return None
pre = dumpy
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
双指针
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
var pre *ListNode
cur := head
for cur != nil{
behind := cur.Next
cur.Next = pre
pre = cur
cur = behind
}
return pre
}
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func reverseList(head *ListNode) *ListNode {
var pre, cur *ListNode = nil, head
for cur != nil {
next := cur.Next
cur.Next = pre
pre = cur
cur = next
}
return pre
}
共以下 61 个回答
双指针同步移动,相差一位,据此修改链表结点指向
时间复杂度:O(N)
空间复杂度:O(1)
两种解法:
1、双指针pre 在前,cur在后,不断改变指针方向
2、递归 每次递归的参数也是 pre 和 cur,curnull,开始返回,返回的是最后节点中间递归不改变这个节点只当作结果返回。
注意每次回归到上一层,指向的cur 和 pre节点都是不同的,都是前面的上一节点
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
// class Solution {
// public ListNode reverseList(ListNode head) {
// return recur(null,head);
// }
// public ListNode recur(ListNode pre,ListNode cur){
// if(curnull){
// return pre;
// }
// ListNode newHead =recur(cur,cur.next);
// cur.next=pre;
// return newHead;
// }
// }
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
ListNode temp=null;
}
// 迭代法,时间复杂度o1,空间复杂度on
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
}
// 时间复杂度on 空间复杂度on 递归深度达到 on
public ListNode reverseList(ListNode head) {
return reverse(head, null);
}
private ListNode reverse(ListNode cur, ListNode pre) {
if (cur null) return pre;
ListNode res = reverse(cur.next, cur);
cur.next = pre;
return res;
}
递归
时间复杂度O(n),空间复杂度O(n)
迭代(原地反转)
时间复杂度O(n),空间复杂度O(1)
原地反转+递归
时间复杂度O(n)空间复杂度O(1)用了迭代的方法
public ListNode reverseList(ListNode head) {
//原地 肯定用插头法
ListNode pre =new ListNode();
ListNode temp ;
pre.next=null;
while(head!=null)
{
temp=head;
head=head.next;
temp.next=pre.next;
pre.next=temp;
代码:
双指针
考虑遍历链表,并在访问各节点时修改 next 引用指向
时间复杂度
O(n):遍历链表使用时间
空间复杂度
O(1):变量pre 和 cur 使用常数大小的额外空间
原地调转。
原地调转。
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
#dumpy=ListNode(None)
dumpy = None
if head None:
return None
pre = dumpy
cur = head
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
双指针
public ListNode reverseList(ListNode head) {
if(headnull || head.next null)return head;
ListNode temp = reverseList(head.next);
head.next.next=head;
head.next=null;
return temp;
}
时间复杂度、空间复杂度都为O(n)
打卡:
class linkelist:
打卡
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// C++实现,反转链表
class Solution {
public:
ListNode reverseList(ListNode* head) {
if (head nullptr || head->next nullptr) {
return head;
}
ListNode* reverseListHead = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return reverseListHead;
};
时间复杂度:O(n);
空间复杂度:O(n);
代码
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n)
空间复杂度:O(1)
“`c++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur,*pre,*nex;
cur=head;
pre=NULL;
while(cur)
{
nex=cur->next;
cur->next=pre;
pre=cur;
cur=nex;
}
return pre;
}
};
“`
二
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head null || head.next null) {
return head;
}
ListNode cur = head, pre = null;
while (cur != null) {
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
}
}
时间复杂度:O(n)
空间复杂度:O(1)
递归写法
时间复杂度:O(n)
空间复杂度:O(n)
原地反转
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度 O(n) 空间复杂度 O(1)
原地反转链表, O(n)时间 O(1)空间。
递归法, 双O(n)
估计也就我用TS了
“`cpp
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head||!head->next)return head;
};
”’递归
“`cpp
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head||!head->next)return head;
};
“`
原地翻转
c++ 原地翻转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(headNULL) return NULL;
ListNode* pre=NULL,* ne=NULL,*cur=head;
while(cur)
{
ne=cur->next;
cur->next=pre;
pre=cur;
cur=ne;
}
return pre;
}
};
方法一
原地反转。使用双指针对前后两个节点进行反转,依次进行。
方法二
递归
利用迭代循环
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(1)
思路:本题需要分类讨论,考虑当链表长度为2和长度大于2两种情况
当长度大于2是要注意用两个指针时,需要在循环的最后再进行指针的变换
代码如下
时间复杂度:O(1)
空间复杂度:O(n)
”’java
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
while(head != null){
ListNode next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
”’
时间复杂度:O(n)
空间复杂度:O(1)
迭代、递归
以下是代码
迭代
头插法反转:
时间复杂度:O(n)
空间复杂度:O(1)
递归法
时间复杂度:O(n)
空间复杂度:O(n)
固定步骤,直接理解记住就行C++
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pt=head;
ListNode* n=NULL;
ListNode* tp;
while(pt){
tp=pt->next;
pt->next=n;
n=pt;
pt=tp;
}
return n;
};
从第一个开始反转,一个一个来,不要着急,条理清晰。
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
Day2:下面是代码
#双指针
时间复杂度:O(n)
空间复杂度:O(1)
时间复杂度:O(n)
空间复杂度:O(n)