203.移除链表元素
- 注意头指针与头结点的区别,头指针不用malloc分配内存头节点去要malloc一个内存
- 还是要注意链表创建的语法
a. 本体采用虚拟头节点法,注意是头节点而不是指针
如下便是建立一个新的节点并将其地址分配给指针,语法上分配地址不用先对指针解引用,解引用未分配地址的指针会出现错误。 注意后面新建链表节点写法 这里其实是两步,建一个节点,指针指向该节点
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
b.自己先前的疑惑为什么有的shead指针需要malloc但是其他的不用,是之后对起操作的dummy->next = head;中他自己不能是空指针必须先有指向
(练的还是太少,结束了回来再看看)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
dummy->next = head;//虚拟头节点指向个头结点
struct ListNode* cur = dummy;
while(cur->next!=NULL)
{
if(cur->next->val == val)
{
struct ListNode* temp = cur->next;
cur->next = cur->next->next;
free(temp);
}
else cur = cur->next;
}
head = dummy->next;
free(dummy);
return head;
}
707.设计链表
难在写
//看样子obj是头节点
typedef struct MyLinkedList {
int val;
struct MyLinkedList* next;
}MyLinkedList;
/** Initialize your data structure here. */
MyLinkedList* myLinkedListCreate() {
//这个题必须用虚拟头指针,参数都是一级指针,头节点确定后没法改指向了!!!
MyLinkedList* head = (MyLinkedList *)malloc(sizeof (MyLinkedList));
head->next = NULL;
return head;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int myLinkedListGet(MyLinkedList* obj, int index) {
MyLinkedList *cur = obj->next;
for (int i = 0; cur != NULL; i++){
if (i == index){
return cur->val;
}
else{
cur = cur->next;
}
}
return -1;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyLinkedList *nhead = (MyLinkedList *)malloc(sizeof (MyLinkedList));
nhead->val = val;
nhead->next = obj->next;
obj->next = nhead;
}
/** Append a node of value val to the last element of the linked list. */
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList *cur = obj;
while(cur->next != NULL){
cur = cur->next;
}
MyLinkedList *ntail = (MyLinkedList *)malloc(sizeof (MyLinkedList));
ntail->val = val;
ntail->next = NULL;
cur->next = ntail;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if (index == 0){
myLinkedListAddAtHead(obj, val);
return;
}
MyLinkedList *cur = obj->next;
for (int i = 1 ;cur != NULL; i++){
if (i == index){
MyLinkedList* newnode = (MyLinkedList *)malloc(sizeof (MyLinkedList));
newnode->val = val;
newnode->next = cur->next;
cur->next = newnode;
return;
}
else{
cur = cur->next;
}
}
}
/** Delete the index-th node in the linked list, if the index is valid. */
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
if (index == 0){
MyLinkedList *tmp = obj->next;
if (tmp != NULL){
obj->next = tmp->next;
free(tmp);
}
return;
}
MyLinkedList *cur = obj->next;
for (int i = 1 ;cur != NULL && cur->next != NULL; i++){
if (i == index){
MyLinkedList *tmp = cur->next;
if (tmp != NULL) {
cur->next = tmp->next;
free(tmp);
}
return;
}
else{
cur = cur->next;
}
}
}
void myLinkedListFree(MyLinkedList* obj) {
while(obj != NULL){
MyLinkedList *tmp = obj;
obj = obj->next;
free(tmp);
}
}
/**
* Your MyLinkedList struct will be instantiated and called as such:
* MyLinkedList* obj = myLinkedListCreate();
* int param_1 = myLinkedListGet(obj, index);
* myLinkedListAddAtHead(obj, val);
* myLinkedListAddAtTail(obj, val);
* myLinkedListAddAtIndex(obj, index, val);
* myLinkedListDeleteAtIndex(obj, index);
* myLinkedListFree(obj);
*/
206.反转链表
原理图:
递归
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverse(struct ListNode* cur,struct ListNode * pre)
{
if(!cur)
return pre;
struct ListNode* temp = cur->next;
cur->next = pre;
return reverse(temp,cur);//等价进入下一层循环
}
struct ListNode* reverseList(struct ListNode* head){
return reverse(head,NULL);
}
双指针
注意temp指针后temp与cur的赋值顺序
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* pre;
struct ListNode* cur;
pre = NULL;
cur = head;
while(cur!=NULL)
{
struct ListNode* temp;
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}