206、反转链表
用迭代法,将个节点的值往前插 如 1->2->3->4->5 new一个Null节点 第一个节点1->NULL head=head->next 第二个节点2->1->NULL就这样前插 /** Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; / class Solution { public: ListNode reverseList(ListNode* head) { ListNode* list=NULL; while(head!=NULL) { ListNode* node=new ListNode(-1); node->val=head->val; node->next=list; list=node; head=head->next; } return list; } }; 当然,最容易想到的方法是将链表里面的数字扔进容器,然后反向生成一个链表就行。 用栈也行 /** Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next