找关键词的网站,wordpress自定义登录地址,wordpress绝对域名更改,怎么做好推广Topic 1#xff1a;LeetCode——203. 移除链表元素203. 移除链表元素 - 力扣#xff08;LeetCode#xff09;移除链表中的数字6操作很简单#xff0c;我们只需要把2的指向地址修改就好了#xff0c;原来的指向地址是6现在改为3这个思路是完全正确的#xff0c;但是在链表…Topic 1LeetCode——203. 移除链表元素203. 移除链表元素 - 力扣LeetCode移除链表中的数字6操作很简单我们只需要把2的指向地址修改就好了原来的指向地址是6现在改为3这个思路是完全正确的但是在链表中增加或删除元素是很麻烦的我们需要判断前后是否为空指针情况很多。我们利用这个思路可以推广另外的做法把不是6的元素尾插到一个新链表这样就可以去除所有6/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* removeElements(struct ListNode* head, int val)
{if(headNULL){return NULL;}struct ListNode* cur head;//利用另外的变量遍历//哨兵防止空指针struct ListNode* newnode(struct ListNode*)malloc(sizeof(struct ListNode));newnode-nextNULL;struct ListNode* tailnewnode;//记录下次尾插的作用while(cur){if(cur-val!val){tail-nextcur;//尾插到哨兵后面tailtail-next;//尾向后curcur-next;//指针向后}else{struct ListNode* tmpcur-next;//存下一个我们释放当前位置free(cur);curtmp;}}tail-nextNULL;//到尾了headnewnode-next;//去除哨兵free(newnode);return head;}Topic 2LeetCode——876. 链表的中间结点876. 链表的中间结点 - 力扣LeetCode找中间很简单但是假如我们要求时间复杂度为O(N)我们应该怎么去做小明一次走一步小华一次走两步当小华走完全程的时候小明走的路程刚刚好是全部路程的一半。通过这个例子我们可以明白我们可以定义两个指针来进行查找中间的节点。这种方法叫做快慢指针。/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* fasthead;struct ListNode* slowhead;while(fast fast-next)//当元素是奇数个时fast-next为NULL;当元素为偶数个fast为NULL是结束标志{fastfast-next-next;slowslow-next;}return slow;
}Topic 3链表中倒数第k个结点链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)链表的倒数第k个元素我们先处理极端情况链表为空的时候k大于链表元素个数把极端情况处理完我们思考当时间复杂度为O(N)时我们应该怎么样得到倒数第k的节点。倒数第k个就是正数的第n-k个我们定义两个指针一个先走k步然后两个指针同时走当先走k步的指针到结尾的时候我们第二个指针就是走到了n-k的位置。/*** struct ListNode {* int val;* struct ListNode *next;* };*//*** * param pListHead ListNode类 * param k int整型 * return ListNode类*/
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k )
{struct ListNode* fastpListHead;struct ListNode* slowpListHead;if(pListHeadNULL){return NULL;}while(k--)//先走k步{if(fastNULL)//链表的元素小于k{return NULL;}fastfast-next;}while(fast){fastfast-next;slowslow-next;}return slow;}Topic 4链表分割我们先解读题目给一个链表给定一个值x大于x的放后面小于x的放前面。不改变原来的数据顺序即原来小于x的值是4 2 1我们不改变想对位置 4 2 1.题目理解清楚了我们怎么做呢我们创建两个链表一个存小于x的值一个存大于等于x的值然后连接起来这样他们的相对位置就没有改变。/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:ListNode* partition(ListNode* pHead, int x){struct ListNode* gguard;//存大于等于x的数据得哨兵struct ListNode* lguard;//存小于x的哨兵struct ListNode* gtail;//存大于x的尾struct ListNode* ltail;//存小于x的尾struct ListNode* curpHead;gguardgtail(struct ListNode*)malloc(sizeof(struct ListNode));//申请哨兵lguardltail(struct ListNode*)malloc(sizeof(struct ListNode));//申请哨兵gtail-nextltail-nextNULL;//哨兵后面一个置空while(cur){if(cur-valx){gtail-nextcur;//哨兵后面接插入的链表gtailgtail-next;//尾向后移}else{ltail-nextcur;ltailltail-next;}curcur-next;}ltail-nextgguard-next;//连接两个链表gtail-nextNULL;//新链表的最后要置空pHeadlguard-next;free(lguard);free(gguard);return pHead;}
};Topic 5 反转链表206. 反转链表 - 力扣LeetCode反转链表我们可以利用头插的思路即我们建立一个新的链表把一个一个元素头插到第一个最后插入的元素就是新的头第一个插入的元素就是新的尾。头插最最要的是更新新的头新的头就是新插入的元素的地址。/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/struct ListNode* reverseList(struct ListNode* head)
{if(headNULL){return NULL; }struct ListNode* curhead;struct ListNode* newnodeNULL;while(cur){struct ListNode* nextcur-next;//存下一个防止找不到cur-nextnewnode;//将链表第一个元素指向newnode指针下面依次指向newnodecur;//更新newnode让newnode一直指向链表的开头curnext;//链表向后移}return newnode;}Topic 6链表的回文结构链表的回文结构_牛客题霸_牛客网 (nowcoder.com)链表的回文怎么判断。我们最直接的方法是找到中间节点反转中间节点得到翻转的链表与前半段链表比较。如果前半段链表与后半段链表翻转后的元素都相等说明这个链表是回文链表。/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/struct ListNode* reverseList(struct ListNode* head)
{if(headNULL){return NULL;}struct ListNode* curhead;struct ListNode* newnodeNULL;while(cur){struct ListNode* nextcur-next;cur-nextnewnode;newnodecur;curnext;}return newnode;}struct ListNode* middleNode(struct ListNode* head)
{struct ListNode* fasthead;struct ListNode* slowhead;while(fast fast-next)//当元素是奇数个时fast-next为NULL;当元素为偶数个fast为NULL是结束标志{fastfast-next-next;slowslow-next;}return slow;
}class PalindromeList {
public:bool chkPalindrome(ListNode* head) {struct ListNode* midmiddleNode(head);//找到中间节点struct ListNode* rheadreverseList(mid);//反转中间节点后的元素while(rhead){if(head-valrhead-val){headhead-next;rheadrhead-next;}else{return false;//不相等说明不是回文}}return true;}
};Topic 7相交链表160. 相交链表 - 力扣LeetCode怎么判断链表相交我们需要看链表的地址是否有相等的如果我们利用两个for循环来实现判断是否有相等的地址我们的时间复杂度就是O(N^2),,这个时间复杂度是很大的。我们还能通过什么方法来判断呢相交的链表有共同的特点就是它们有共同的尾我们只需要判断尾的地址是否相等就好如果尾的地址相等我们就说明它们是相交的链表。相交的链表如何寻找第一个交点全部遍历一遍这个好像不太可能。既然链表有交点第一个交点到链表尾的距离相等而头到链表交点的距离就不一定相等了它们的差值刚刚好是两个链表距离的差值。我们让长链表先走它们的差值然后一起走等它们的地址相等的时候就是它们的第一个交点。/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB)
{struct ListNode* cur1headA;struct ListNode* cur2headB;int n11;//赋值1是因为统计链表长度的时候我们的最后的一个位置我们统计到int n21;while(cur1-next)//找headA的尾{cur1cur1-next;n1;}while(cur2-next)//找headB的尾{cur2cur2-next;n2;}if(cur1!cur2){return NULL;}int difabs(n1-n2);//让长的先走cur1headA;cur2headB;if(n1n2){while(dif){cur1cur1-next;dif--;}}else{while(dif){cur2cur2-next;dif--;}}while(cur1!cur2){cur1cur1-next;cur2cur2-next;}return cur1;}