203.移除链表元素
1.创建虚拟头节点 , dummy =new ListNode(0); dummy = malloc(sizeof(ListNode));
2.cur = dummy ,看删除值的前一个值
3.终止条件 cur->next !=NULL
4.cur = cur->next;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
typedef struct ListNode ListNode;
ListNode * dummy = malloc(sizeof(ListNode));
dummy->next = head;
ListNode *cur = dummy;
while(cur->next!=NULL)
{
if(cur->next->val == val){
ListNode * tmp = cur->next;
cur->next = tmp->next;
free(tmp);
}else{
cur = cur->next;
}
}
head = dummy->next;
free(dummy);
return head;
}
C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
//创建dummy
ListNode * dummy = new ListNode(0);
dummy->next = head;
ListNode * cur = dummy;
while(cur->next!=NULL){
if(cur->next->val == val)
{
ListNode*tmp = cur->next;
cur->next = tmp->next;
delete tmp;
}else{
cur = cur->next;
}
}
head = dummy->next;
delete dummy;
return head;
}
};
#707.设计链表
题目中的index 跟自己理解的有偏差 ,还得理解一下
C
typedef struct Linklist{
int val;
struct Linklist * next;
} MyLinkedList;
MyLinkedList* myLinkedListCreate() {
MyLinkedList* dummy = malloc(sizeof(MyLinkedList));
dummy->next = NULL;
return dummy;
}
int myLinkedListGet(MyLinkedList* obj, int index) {
MyLinkedList* cur = obj->next;
for(int i =0;i<index&& cur!=NULL ; i++)
{
cur = cur->next;
}
if(cur ==NULL)
{
return -1;
}else
{
return cur->val;
}
}
void myLinkedListAddAtHead(MyLinkedList* obj, int val) {
MyLinkedList* newnode = malloc(sizeof(MyLinkedList));
newnode->val = val;
newnode->next = obj->next;
obj->next = newnode;
}
void myLinkedListAddAtTail(MyLinkedList* obj, int val) {
MyLinkedList* cur =obj;
while(cur->next!=NULL){
cur = cur->next;
}
MyLinkedList *ntail = malloc(sizeof (MyLinkedList));
ntail->val = val;
ntail->next = NULL;
cur->next = ntail;
}
void myLinkedListAddAtIndex(MyLinkedList* obj, int index, int val) {
if (index == 0){
myLinkedListAddAtHead(obj, val);
return;
}
MyLinkedList * cur= obj->next;
for(int i =0; cur!=NULL;i++)
{
if(i==index-1)
{
MyLinkedList* newnode = (MyLinkedList *)malloc(sizeof (MyLinkedList));
newnode->val = val;
newnode->next = cur->next;
cur->next = newnode;
return;
}else{
cur = cur->next;
}
}
}
void myLinkedListDeleteAtIndex(MyLinkedList* obj, int index) {
MyLinkedList * cur= obj->next;
for(int i =0; cur!=NULL&&cur->next!=NULL;i++)
{
if(i==index-1)
{
MyLinkedList* tmp = cur ->next;
cur->next = tmp->next;
free(tmp);
return;
}else if(cur==NULL||cur->next==NULL){
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);
*/
C++
后面补充
#206.反转链表
双指针法
pre, cur, tmp保存下一个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
typedef struct ListNode ListNode;
ListNode* tmp,*cur,*pre;
pre = NULL;
cur=head;
while(cur!=NULL)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
递归法:
从前往后反转指针
class Solution {
public:
ListNode* reverse(ListNode* pre,ListNode* cur){
if(cur == NULL) return pre;
ListNode* temp = cur->next;
cur->next = pre;
// 可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步
// pre = cur;
// cur = temp;
return reverse(cur,temp);
}
ListNode* reverseList(ListNode* head) {
// 和双指针法初始化是一样的逻辑
// ListNode* cur = head;
// ListNode* pre = NULL;
return reverse(NULL, head);
}
};
从后往前翻转指针指向 ,后面再看
class Solution {
public:
ListNode* reverseList(ListNode* head) {
// 边缘条件判断
if(head == NULL) return NULL;
if (head->next == NULL) return head;
// 递归调用,翻转第二个节点开始往后的链表
ListNode *last = reverseList(head->next);
// 翻转头节点与第二个节点的指向
head->next->next = head;
// 此时的 head 节点为尾节点,next 需要指向 NULL
head->next = NULL;
return last;
}
};