牛客101 1 链表反转

牛客101 1 链表反转_第1张图片/*
public class ListNode {
int val;
ListNode next = null;

ListNode(int val) {
    this.val = val;
}

}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
// write code here
if(headnull){return null;}
if(head.next
null){return head; }
ListNode prev=head;
ListNode cur=null;
while(prev!=null)
{ListNode prev_next=prev.next;
prev.next=cur;
cur=prev;
prev=prev_next;

}
return cur;
}

}

你可能感兴趣的:(链表,数据结构,算法)