我C都没学会,直接统一用C++,坑苦我了
算了,归根结底 都是自己太弱的锅,
/**
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param head: n
* @return: The new head of reversed linked list.
*/
ListNode * new_node(ListNode * head,int val){
ListNode * a=new ListNode(val);
a->next=head;
head=a;
return head;
} //建立一个从右到左生成新节点的函数
ListNode * reverse(ListNode * head) {
// write your code here
ListNode *p=head,*new_head=NULL;
for(;p!=NULL;){
new_head=new_node(new_head,p->val);
p=p->next;
} //从左到右读取要翻转的链表
head=new_head;
return head;
}
};