当前位置: 首页 > news >正文

娱乐网站建设公司排名o2o好网站

娱乐网站建设公司排名,o2o好网站,如何做视频卖给网站,网页设计基础代码网站目录面试题 02.04 分割链表剑指 Offer II 027 回文链表160 相交链表141 环形链表142 环形链表 II138 复制带随机指针的链表面试题 02.04 分割链表 定义lesshead和greaterhead链接小于和大于等于k的值分别设置哨兵位和尾节点指针最后将两表去除哨兵位再链接 struct ListNode* p…

目录

  • 面试题 02.04 分割链表
  • 剑指 Offer II 027 回文链表
  • 160 相交链表
  • 141 环形链表
  • 142 环形链表 II
  • 138 复制带随机指针的链表

面试题 02.04 分割链表

  • 定义lesshead和greaterhead链接小于和大于等于k的值
  • 分别设置哨兵位和尾节点指针
  • 最后将两表去除哨兵位再链接

在这里插入图片描述

struct ListNode* partition(struct ListNode* head, int x)
{struct ListNode* lesshead, * lesstail, * greaterhead, * greatertail;lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));lesstail->next = NULL;greaterhead = greatertail = (struct ListNode*)malloc(sizeof(struct ListNode));greatertail->next = NULL;struct ListNode* cur = head;while (cur){if (cur->val < x){lesstail->next = cur;lesstail = cur;}else{greatertail->next = cur;greatertail = cur;}cur = cur->next;}lesstail->next = greaterhead->next;greatertail->next = NULL;struct ListNode* newhead = lesshead->next;free(lesshead);free(greaterhead);return newhead;
}

 

剑指 Offer II 027 回文链表

  • 先找到链表中间节点
  • 将中间节点以后的节点从原链表截断逆置生成新链表
  • 与原链表逐节点的值相比较
    在这里插入图片描述
//回文结构
bool isPalindrome(struct ListNode* head) {if (head == NULL){return false;}//找中间节点struct ListNode* cur = head, * slow = head, * fast = head;while (fast && fast->next){slow = slow->next;fast = fast->next->next;}struct ListNode* mid = slow;//将中间节点之后的顺序反转struct ListNode* newhead = slow;cur = newhead;struct ListNode* prev = NULL;while (cur){struct ListNode* next = cur->next;cur->next = prev;prev = cur;cur = next;}newhead = prev;//遍历两组链表,检查是否每位相等struct ListNode* cur2 = newhead;struct ListNode* cur1 = head;while (cur1 && cur2){if (cur1->val != cur2->val){return false;}cur1 = cur1->next;cur2 = cur2->next;}return true;
}

 

160 相交链表

  • 根据两链表长度求出长度差k
  • 较长的链表先走k步
  • 两表再一起走,节点地址相同时返回此节点
 int ListSize(struct ListNode * head){struct ListNode * cur=head;int len=0;while(cur){len++;cur=cur->next;}return len;}
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {int len1=ListSize(headA);int len2=ListSize(headB);int k=abs(len1-len2);struct ListNode * longlist=headA;struct ListNode * shortlist=headB;if(len1<len2){longlist=headB;shortlist=headA;}struct ListNode * cur1=longlist, *cur2=shortlist;while(k--){cur1=cur1->next;}while(cur1 && cur2){if(cur1==cur2){return cur1;}cur1=cur1->next;cur2=cur2->next;}return NULL;
}

 

141 环形链表

快慢指针法,相遇则为环形链表

//环形链表,快慢指针法
bool hasCycle(struct ListNode* head) {struct ListNode* slow = head;struct ListNode* fast = head;while (fast && fast->next){fast = fast->next->next;slow = slow->next;if (slow == fast){return true;}}return false;
}

142 环形链表 II

  • 先找到入环点
  • 相遇点到入环点的距离等于链头到入环点的距离
  • 从链头和相遇点出发,相遇点即为入环点

在这里插入图片描述

//环形链表 II
//找到入环点,再判断环形链表代码块内续写
struct ListNode* detectCycle(struct ListNode* head) {struct ListNode* slow = head, * fast = head, * meet = NULL;while (fast && fast->next){//找到快慢指针相遇点fast = fast->next->next;slow = slow->next;//相遇点到入环点的距离等于链头到入环点的距离if (slow == fast){meet = slow;while (meet != head){meet = meet->next;head = head->next;}return meet;}}return NULL;
}

138 复制带随机指针的链表

  • 在原链表每个节点后复制一个节点
  • 根据原节点设置复制节点的random
    • 注意不可复制节点的同时处理random,因为random指向位置还未完成复制
  • 将处理好的复制节点链接到newhead
    在这里插入图片描述
//复制带随机指针的链表
struct Node* copyRandomList(struct Node* head) {struct Node* cur = head;//在原链表每个节点后复制一个节点while (cur){struct Node* copy = (struct Node*)malloc(sizeof(struct Node));copy->val = cur->val;copy->next = cur->next;cur->next = copy;cur = copy->next;}cur = head;struct Node* next = NULL;//根据原节点设置复制节点的randomwhile (cur){struct Node* copy = cur->next;next = copy->next;if (cur->random == NULL){copy->random = NULL;}else{copy->random = cur->random->next;}cur = next;}//将处理好的复制节点链接到newheadcur = head;struct Node* newtail = NULL, * newhead = NULL;while (cur){struct Node* copy = cur->next;next = copy->next;if (newtail == NULL){newhead = newtail = copy;}else{newtail->next = copy;newtail = copy;}cur->next = next;cur = next;}return newhead;
}
http://www.15wanjia.com/news/193200.html

相关文章:

  • 建设工业网站首页网站建设的电话销售好做吗
  • 岳阳云溪区建设局网站购物网站后台管理模板
  • 济南网站制作培训班小学生抄写新闻20字
  • 深圳建设工程项目网站做网站用哪种语言好
  • 自己的电脑做服务器 并建网站哈尔滨公共资源交易中心官网
  • 家装网站做wordpress商城 微信
  • 兰州市建设工程安全质量监督站网站WordPress优惠券主题
  • 入户广州网站福州小程序开发公司列表
  • 网站排名优化软件哪家好全美网站建设公司
  • 零基础网站建设教程漯河网络推广哪家好
  • 绿色网站模板大全怎么建立一个网站?
  • 网站dns解析动态ip做网站影响seo吗
  • 网站怎么做悬浮图片放大无锡做公司网站多少钱
  • 网站备案没了免费seo营销优化软件下载
  • 吴江盛泽建设局网站福州网站推广公司
  • 长治市住房保障和城乡建设管理局网站网络运维工程师简历范文
  • 韶关哪里做网站最好公司的网站建设哪家比较好
  • 金融网站怎么做在线图片编辑软件免费版
  • 做emc的有哪些网站仓储网站模板
  • 陕西网站建设方案教育培训网
  • 深圳微商城网站设计国内类似wordpress
  • 成都专业网站建设哪家好wordpress文章更新插件
  • 包头网站网站建设原理试卷
  • 长春建设网站招标代理公司注册
  • 郑州网站建设网络公司如何创建企业邮箱
  • 菏泽市城乡和建设局网站2017做淘宝客网站还有吗
  • 网站建设模板的鄂州免费设计网站建设
  • 湖北联兴建设有限公司网站app手机网站建设黄
  • 北仑网站推广wordpress 调用所有分类
  • 网站流量图怎么做的微信网站应用开发