赞
踩
反转一个单链表:一种解决方案是按原始顺序迭代结点,并将它们逐个移动到列表的头部。
C
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* prev = NULL;
struct ListNode* curr = head;
while (curr) {
struct ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
C++
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* prev = nullptr;
ListNode* curr = head;
while (curr) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
C
/** * 以链表1->2->3->4->5举例 * @param head * @return */ public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { /* 直到当前节点的下一个节点为空时返回当前节点 由于5没有下一个节点了,所以此处返回节点5 */ return head; } //递归传入下一个节点,目的是为了到达最后一个节点 ListNode newHead = reverseList(head.next); /* 第一轮出栈,head为5,head.next为空,返回5 第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4, 把当前节点的子节点的子节点指向当前节点 此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null 此时链表为1->2->3->4<-5 返回节点5 第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3, 此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null 此时链表为1->2->3<-4<-5 返回节点5 第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2, 此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null 此时链表为1->2<-3<-4<-5 返回节点5 第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1, 此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null 此时链表为1<-2<-3<-4<-5 返回节点5 出栈完成,最终头节点5->4->3->2->1 */ head.next.next = head; head.next = null; return newHead; }
C++
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !head->next) {
return head;
}
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return newHead;
}
};
移除头结点和移除其他节点的操作是不一样的,因为链表的其他节点都是通过前一个节点来移除当前节点,而头结点没有前一个节点。
所以头结点如何移除呢,其实只要将头结点向后移动一位就可以,这样就从链表中移除了一个头结点。依然别忘将原头结点从内存中删掉。这样移除了一个头结点,是不是发现,在单链表中移除头结点 和 移除其他节点的操作方式是不一样,其实在写代码的时候也会发现,需要单独写一段逻辑来处理移除头结点的情况。
C代码
truct ListNode* removeElements(struct ListNode* head, int val){ struct ListNode* temp; // 当头结点存在并且头结点的值等于val时 while(head && head->val == val) { temp = head; // 将新的头结点设置为head->next并删除原来的头结点 head = head->next; free(temp); } struct ListNode *cur = head; // 当cur存在并且cur->next存在时 // 此解法需要判断cur存在因为cur指向head。若head本身为NULL或者原链表中元素都为val的话,cur也会为NULL while(cur && (temp = cur->next)) { // 若cur->next的值等于val if(temp->val == val) { // 将cur->next设置为cur->next->next并删除cur->next cur->next = temp->next; free(temp); } // 若cur->next不等于val,则将cur后移一位 else cur = cur->next; } // 返回头结点 return head; }
C++代码
class Solution { public: ListNode* removeElements(ListNode* head, int val) { // 删除头结点 while (head != NULL && head->val == val) { // 注意这里不是if ListNode* tmp = head; head = head->next; delete tmp; } // 删除非头结点 ListNode* cur = head; while (cur != NULL && cur->next!= NULL) { if (cur->next->val == val) { ListNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; } else { cur = cur->next; } } return head; } };
其实可以设置一个虚拟头结点,这样原链表的所有节点就都可以按照统一的方式进行移除了。
来看看如何设置一个虚拟头。依然还是在这个链表中,移除元素1。这里来给链表添加一个虚拟头结点为新的头结点,此时要移除这个旧头结点元素1。
C代码
struct ListNode* removeElements(struct ListNode* head, int val){ typedef struct ListNode ListNode; ListNode *shead; shead = (ListNode *)malloc(sizeof(ListNode)); shead->next = head; ListNode *cur = shead; while(cur->next != NULL){ if (cur->next->val == val){ ListNode *tmp = cur->next; cur->next = cur->next->next; free(tmp); } else{ cur = cur->next; } } head = shead->next; free(shead); return head; }
C++代码
class Solution { public: ListNode* removeElements(ListNode* head, int val) { ListNode* dummyHead = new ListNode(0); // 设置一个虚拟头结点 dummyHead->next = head; // 将虚拟头结点指向head,这样方面后面做删除操作 ListNode* cur = dummyHead; while (cur->next != NULL) { if(cur->next->val == val) { ListNode* tmp = cur->next; cur->next = cur->next->next; delete tmp; } else { cur = cur->next; } } head = dummyHead->next; delete dummyHead; return head; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。