定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode reverseList(ListNode head) {
ListNode node=head,pre=null;
while(node!=null){
ListNode tmp = node.next;
node.next=pre;
pre=node;
node=tmp;
}
return pre;
}
}