203.移除链表元素
当链表中节点满足 current.val == val
时
由于节点不是我们用malloc创建的,题目中也没要求,删除的节点就不用管了
题解
struct ListNode* removeElements(struct ListNode* head, int val){
struct ListNode* currentNode = head;
struct ListNode* previousNode;
struct ListNode tempNode;
tempNode.next = head;
previousNode = &tempNode;
while(currentNode != NULL)
{
if(currentNode->val == val)
{
previousNode->next = currentNode->next;
currentNode = currentNode->next;
}
else
{
previousNode = currentNode;
currentNode = currentNode->next;
}
}
return tempNode.next;
}
707.设计链表
typedef用多了,一开始这样定义类型的,
typedef struct {
int val;
MyLinkedList* next;
} MyLinkedList;
报错:
error: unknown type name ‘MyLinkedList’
正确定义节点类型是
typedef struct MyListNode
{
int val;
struct MyListNode* next;
}MyListNodeType;
想再定义一个链表对象,包含链表head节点和链表长度,
typedef struct MyListNode
{
int val;
struct MyListNode* next;
}MyListNodeType;
typedef struct
{
MyListNodeType* head;
int ListLen;
}MyLinkedList;
MyLinkedList* myLinkedListCreate() {
MyLinkedList* MyListObj = (MyLinkedList*)malloc(sizeof(MyLinkedList));
MyListObj->head = NULL;
MyListObj->ListLen = 0;
return MyListObj;
}
没有写成,以下是错误代码,回头再看
Line 30: Char 13: runtime error: member access within null pointer of type 'struct MyListNodeType' [solution.c]
typedef struct MyListNode
{
int val;
struct MyListNode* next;
}MyListNodeType;
typedef struct
{
MyListNodeType* head;
int ListLen;
}MyLinkedList;
MyLinkedList* myLinkedListCreate() {
MyLinkedList* MyListObj = (MyLinkedList*)malloc(sizeof(MyLinkedList));
MyListObj->head = NULL;
MyListObj->ListLen = 0;
return MyListObj;
}
int myLinkedListGet(MyLinkedList* obj, int index) {
int i;
int ret = -1;
MyListNodeType* currNode = obj->head;
if(index < obj->ListLen)
{
for(i = 0; i < index; i++)
{
currNode = currNode->next;
}
ret = currNode->val;
}
return ret;
}
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyListNodeType* newNode= (MyListNodeType*)malloc(sizeof(MyListNodeType));
newNode->val = val;
newNode->next = obj->head;
obj->head = newNode;
(obj->ListLen)++;
}
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
int i;
MyListNodeType* currentNode;
MyListNodeType* newNode= (MyListNodeType*)malloc(sizeof(MyListNodeType));
newNode->val = val;
newNode->next = NULL;
if(obj->ListLen == 0)
{
obj->head = newNode;
}
else
{
currentNode = obj->head;
for(i = 1; i < obj->ListLen; i++)
{
currentNode = currentNode->next;
}
currentNode->next = newNode;
}
(obj->ListLen)++;
}
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
int i;
MyListNodeType* newNode;
MyListNodeType* currentNode;
MyListNodeType* previousNode;
if(index == 0)
{
myLinkedListAddAtHead(obj, val);
}
else if(index <= obj->ListLen)
{
newNode= (MyListNodeType*)malloc(sizeof(MyListNodeType));
newNode->val = val;
for(i = 0; i < index; i++)
{
previousNode = currentNode;
currentNode = currentNode->next;
}
newNode->next = currentNode;
previousNode->next = newNode;
(obj->ListLen)++;
}
}
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
int i = 0;
MyListNodeType* currentNode = obj->head;
MyListNodeType* previousNode;
if(index < obj->ListLen)
{
if(index == 0)
{
obj->head = currentNode->next;
free(currentNode);
}
else
{
for(i = 0; i < index; i++)
{
previousNode = currentNode;
currentNode = currentNode->next;
}
previousNode->next = currentNode->next;
free(currentNode);
}
(obj->ListLen)--;
}
}
void myLinkedListFree(MyLinkedList* obj) {
MyListNodeType* currentNode = obj->head;
MyListNodeType* temp;
while(currentNode != NULL)
{
temp = currentNode;
currentNode = currentNode->next;
free(temp);
}
free(obj);
}
/**
* 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.反转链表
由于将current的next指向previous后,若不保存此前的current->next,将丢失current的下一个节点,所以要先将current->next保存
将current的下一节点指向previous
将previous和current移动至下一previous和current
开启下一轮处理
结束条件为current为空,将previous返回
题解
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* previous = NULL;
struct ListNode* current = head;
struct ListNode* nextNode;
while(current != NULL)
{
nextNode = current->next;
current->next = previous;
previous = current;
current = nextNode;
}
return previous;
}