一、单链表经典算法
1.1 单链表相关经典算法OJ题1:移除链表元素
在做这道题的时候,采用定义两个指针,然后定义一个临时变量遍历整个链表,如果这个节点的val不等于要删除的值,就再判断头节点是否为空,如果为空插入数据就是使头节点等于未结点等于pcur newhead=newtail=pcur,不为空则将newtail的下一个定位到pcur,再将newtail移到链表未结点。经过这次遍历newtail的next为野指针,需要将它置空。返回头节点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
ListNode* newHead,*newTile;
newHead=newTile=NULL;
ListNode *pcur=head;
while(pcur){
if(pcur->val!=val){
if(newHead==NULL){
newHead=newTile=pcur
}
else{
newTile->next=pcur;
newTile=newTile->next;
}
}
pcur=pcur->next;
}
if(newTile){
newTile->next=NULL;