迭代迭代迭代
晕晕晕
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; } }
python:
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)