牛客BM1.反转链表![在这里插入图片描述](https://img-blog.csdnimg.cn/d4dc694a935447dea067054aa5d5e35a.png)
解题思路:
首先要反转链表,我们必须要知道此结点的前驱结点和后继结点,定义 pre 和 next
结点。反转最简单的两个节点就是以下
多个结点是
实现代码:
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null||head.next==null)
return head;
ListNode pre=null;
while(head!=null){
ListNode next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
}