1.翻转一个链表
2.尾插法,创建dummy保存head的地址,将创建的temp保存到head->next的地址,这样就让head下移,然后指回原来的地址就可以实现链表的翻转
3.
/**
* Definition of ListNode
*
* class ListNode {
* public:
* int val;
* ListNode *next;
*
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The first node of linked list.
* @return: The new head of reversed linked list.
*/
ListNode *reverse(ListNode *head) {
// write your code here
ListNode *dummy=NULL;
while(head!=NULL){
ListNode *temp=head->next;
head->next=dummy;
dummy=head;
head=temp;
}
return dummy;
}
};
4.感想
一开始不会做,只能百度.......看到别人写的代码,好好学习....
4279




被折叠的 条评论
为什么被折叠?



