package ByteDance
;
public class ReverseList {
public class ListNode {
int val
;
ListNode next
= null
;
ListNode(int val
) {
this.val
= val
;
}
}
public ListNode
ReverseList(ListNode head
) {
if(head
==null
||head
.next
==null
){
return head
;
}
ListNode p1
=null
;
ListNode p2
=head
;
ListNode p3
=head
.next
;
while(p2
!=null
){
p2
.next
=p1
;
p1
=p2
;
p2
=p3
;
if(p3
!=null
){
p3
=p3
.next
;
}
}
return p1
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-28180.html