链表的定义
//C语言
//单链表的定义
Struct ListNode{
int val;
ListNode *next;
ListNode(int x): val(int x),next(NULL){}
};
//初始化链表头节点
ListNode *head=new ListNode(5);
//方法2
ListNode *head=new ListNode();
head->val=5;
//C语言如何定义链表
typedef Struct ListNodeT{
int val;
Struct ListNodeT next;
}ListNode;
203移除链表元素
//C语言
struct ListNode* removeElements(struct ListNode* head, int val){
typedef struct ListNode ListNode;
ListNode *dummy;
dummy = (ListNode*)malloc(sizeof(ListNode));
dummy->next = head;
ListNode *cur = dummy;
while(cur->next!=NULL){
if(cur->next->val==val){
ListNode *temp = cur->next;
cur->next = cur->next->next;
free(temp);
}else{
cur = cur->next;
}
}
head = dummy->next;
free(dummy);
return head;
}
主要思路
707设计链表
获取第n个节点的值
1、首先对数值n进行合法性判断,如果小于0或者大于链表的长度就报错
2、添加哑节点
//获取第n个节点的值
cur = dummy->next;
while(n){
cur=cur->next;
n--;
}
return current->val
头部插入节点
newnode = new node();
//先后再前的顺序
newnode->next = dummy->next
dummy->next = newnode;
size++;
尾部插入节点
newnode = new newnode()
cur = dummy;
while(cur->next!=NULL){
cur = cur->next;
}
cur->next = newnode;
第n个节点前插入节点
newnode = new node();
cur = dummy;
while(n){
cur = cur->next;
n--;
}
newnode->next = cur->next;
cur->next = newnode;
size++;
删除第n个节点
dummy->next = head;
cur = dummy;
while(n){
cur = cur->next;
n--;
}
cur->next = cur->next->next;
size--;
C语言代码
typedef Struct MylinkedList{
int val;
struct MyLinkedList * next;
}MyLinkedList;
MyLinkedList *MyLinkedListCreate(){
MyLinkedList *head = (MyLinkedList*)malloc(sizeof(MyLinkedList));
head->next = NULL;
return head;
//获取第n个节点的值
int MyLinkedGet(MyLinkedList *dummy,int n){
cur = dummy->next;
for(i=0;cur->next!=-NULL;i++){
if(i==n){
return cur->val;
}else{
cur = cur->next;
}
}
return -1;
}
//删除头节点
int MylinkedListAddAthead(MylinkedList*dummy,int val){
MylinkedList *newnode=(MylinkedList*)malloc(sizeof(MylinkedList));
newnode->val = val;
newnode->next = dummy->next;
dummy->next = newnode;
}
//添加尾部节点
int MylinkedListAddtail(MylinkedList *dummy,int val){
MylinkedList *cur = dummy;
while(cur->next!=NULL){
cur = cur->next;
}
MylinkedList *newnode = (MylinkedList*)malloc(sizeof(MylinkedList));
newnode->val = val;
//先后再前
newnode->next = NULL;
cur->next = newnode;
//第n个节点前面插入节点
void MylinkedListAtindex(MylinkedList *dummy,int inedx,int val){
if(index==0){
MylinkedListAddAthead(dummy,val);
return;
}
MylinkedList *cur= dummy->next;
for(int i=0;cur->next!=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;
}
}
}
//删除第n个节点
int MylinkedListdelindex(MylinkedList *dummy,int index){
if(index==0){
MylinkedList *temp = obj->next;
if(temp!=NULL){
dummy->next = temp->next;
free(temp);
}
return;
}
MylinkedList *cur = dummy->next;
for(int i=0;cur!=NULL && cur->next!=NULL;i++){
if(i==index){
MylinkedList *temp = cur->next;
cur->next = temp ->next;
free(temp);
}else{
cur = cur->next;
}else{
cur = cur->next;
}
}
}
void myLinkedListFree(MylinkedList* dummy){
while(obj!=NULL){
MylinkedList *temp = dummy;
dummy=dummy->next;
free(temp);
}
}
反转链表
主要思路:双指针
首先是双指针的方法,在原始链表的基础之上直接反转指针的方向
伪代码
pre = NULL;
cur = head;
while(head){
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;