给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围:
要求:空间复杂度
,时间复杂度
。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:
{1,2,3}{3,2,1}{}{}空链表则输出空
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: ListNode) -> ListNode: cur = head pre = None next = None while cur: next = cur.next cur.next = pre pre = cur cur = next return pre
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 # @return ListNode类 # class Solution: def ReverseList(self , head: ListNode) -> ListNode: if not head &nbs***bsp;not head.next: return head h = self.ReverseList(head.next) head.next.next = head head.next = None return h但是递归版本由于用例太长 栈溢出了
class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here curr = pHead prv = None while curr is not None: tmp = curr.next # 反转 curr.next = prv # 更新 curr, prv prv = curr curr = tmp return prv
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if pHead is None:
return pHead
p1 = pHead
p2 = p1.next
pHead.next = None
while p2 is not None:
p3 = p2.next
p2.next = p1
p1 = p2
p2 = p3
return p1 p1 = pHead; p2 = p1.nextpHead.next = Nonep3 = p2.nextp2.next = p1p1 = p2p2 = p3p3 = p2.nextp2.next = p1p1 = p2p2 = p3同第一次、第二次循环,过程省略
p3 = p2.nextp2.next = p1p1 = p2p2 = p3此时,由于 p2 为空,因此退出循环。
函数返回 p1,即为反转后的链表的头。
如果链表为空,则根据程序开头的判断,直接返回 pHead。
如果链表只有一个结点,则在进入循环之前,p2 已经为空,不会开始循环。因此,无需单独判断 pHead.next 是否为空。
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here # 2021/6/12 递归方法内存爆了已扑街 # if not pHead&nbs***bsp;not pHead.next: # return pHead # pre = self.ReverseList(pHead.next) # pHead.next.next=pHead # pHead.next=None # return pre # 迭代法 if not pHead&nbs***bsp;not pHead.next: return pHead cur = pHead pre = None while cur: temp = cur.next cur.next = pre pre = cur cur = temp return pre
class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here p1=None pEnd=None while pHead: p1=pHead.next pHead.next=pEnd pEnd=pHead pHead=p1 return pEnd
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here left=None right=pHead while right: right.next,right,left=left,right.next,right return left
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here last_num = None while pHead is not None: last_num,pHead.next,pHead = pHead,last_num,pHead.next return last_num这里要特别注意,
last_num,pHead.next,pHead这三个变量他们的顺序要有先后,pHead.next 不能在 pHead 后面,不然当pHead先变成了None,那时候就不能给 pHead.next 赋值了
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def __init__(self): self.newHead = None # 返回ListNode def ReverseList(self, pHead): if pHead == None: return None if pHead.next == None: return pHead # write code here if pHead.next.next == None: pHead.next.next = pHead self.newHead = pHead.next pHead.next = None return self.newHead self.ReverseList(pHead.next) pHead.next.next = pHead pHead.next = None return self.newHead
class Solution: # 返回ListNode def ReverseList(self, pHead): # write code here if not pHead: return None prenode = None #新建变量,虚拟的节点,节点的值为None while pHead: temp = pHead.next #当前节点的next保存起来 pHead.next = prenode #当前节点的next指向当前节点的前一个节点 prenode = pHead #把当前节点保存起来 pHead = temp #移到下一个节点 return prenode
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null)
return null;
//head为当前节点,如果当前节点为空的话,那就什么也不做,直接返回null;
ListNode pre = null;
ListNode next = null;
//当前节点是head,pre为当前节点的前一节点,next为当前节点的下一节点
//需要pre和next的目的是让当前节点从pre->head->next1->next2变成pre<-head next1->next2
//即pre让节点可以反转所指方向,但反转之后如果不用next节点保存next1节点的话,此单链表就此断开了
//所以需要用到pre和next两个节点
//1->2->3->4->5
//1<-2<-3 4->5
while(head!=null){
//做循环,如果当前节点不为空的话,始终执行此循环,此循环的目的就是让当前节点从指向next到指向pre
//如此就可以做到反转链表的效果
//先用next保存head的下一个节点的信息,保证单链表不会因为失去head节点的原next节点而就此断裂
next = head.next;
//保存完next,就可以让head从指向next变成指向pre了,代码如下
head.next = pre;
//head指向pre后,就继续依次反转下一个节点
//让pre,head,next依次向后移动一个节点,继续下一次的指针反转
pre = head;
head = next;
}
//如果head为null的时候,pre就为最后一个节点了,但是链表已经反转完毕,pre就是反转后链表的第一个节点
//直接输出pre就是我们想要得到的反转后的链表
return pre;
}
}
public class Solution { public static ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode reversedHead=null; ListNode current=head; ListNode tmp=null; ListNode pre=null; while(current!=null){ tmp=current.next; current.next=pre; if(tmp==null) reversedHead=current; pre=current; current=tmp; } return reversedHead; } }