1.链表的反转
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* newhead = NULL;
ListNode* temp = NULL;
while(head){
temp = head->next;
head->next = newhead;
newhead = head;
head = temp;
}
return newhead;
}
};
需要注意的是:
1.head 直接就是第一个节点;
2.temp =head;
......
head = temp->next;
此种做法是错误的,temp=head,说明temp和head共享一个地址;
2.链表(m,n)之间进行反转