迭代迭代迭代
晕晕晕
java:
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre=null;
ListNode cur=head;
while(cur!=null){
ListNode tmp=cur.next;
cur.next=pre;
pre=cur;
cur=tmp;
}
return pre;
}
}
python3:
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
cur,pre=head,None
while cur:
tmp=cur.next
cur.next=pre
pre=cur
cur=tmp
return pre