141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

解题思路:

需要检查before和after隔一个的情况。因为除了开始时,如果检查的是before和after相邻,那么两个元素成环时,after跑到before后面,也就

不能检查到环了。

bool hasCycle(ListNode *head) {
if (head == NULL)
return false;
ListNode* before = head;
ListNode* after = before->next;
while (after != NULL && after->next != NULL) {
if (after == before)
return true;
before = before->next;
after = after->next->next;
}
return false;
}  

21. Merge Two Sorted Lists

按升序合并两个链表

解题思路:

当l1和l2不空时,比较它们的表头值,放入小的,同时移动指针。当有一个链表为空时,剩下的那个必然是更大的部分,直接连接上就好。

最后,由于初始化时给了一个表头,所以要返回表头下一个点。

ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* result = new ListNode(0);
ListNode* temp = result;
while (l1 != NULL && l2 != NULL) {
if (l1->val < l2->val) {
temp->next = l1;
l1 = l1->next;
} else {
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
if (l1 != NULL)
temp->next = l1;
else
temp->next = l2;
return result->next;
} 

leetcode-26-exercise_linked-list的更多相关文章

  1. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  2. 前端与算法 leetcode 26. 删除排序数组中的重复项

    目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...

  3. [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  4. Java实现 LeetCode 26 删除排序数组中的重复项

    26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...

  5. leetcode 26

    26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  6. LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)

    [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...

  8. LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)

    题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description   从有序数组中移除重 ...

  9. LeetCode(26)题解:Remove Duplicates from Sorted Array

    https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...

  10. [LeetCode]26. 删除排序数组中的重复项(数组,双指针)

    题目 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下 ...

随机推荐

  1. Cache中间件和缓存降级

    Cache中间件和缓存降级 1.前言 surging受到不少.net同学的青睐,也提了不少问题,提的最多的是什么时候集成API 网关,在这里回答大家最近已经开始着手研发,应该在1,2个月内会有个初版A ...

  2. Json规范

    标准格式 书写使用首字母小写驼峰式 {" status":0   //状态 大于0代表正常.小于等于0代表异常 "message":"",/ ...

  3. RK3288开发过程中遇到的问题点和解决方法之Kernel

    修改背光改变区间 kernel\drivers\video\backlight\pwm_bl.c static int pwm_backlight_update_status(struct backl ...

  4. uvm_reg_backdoor——寄存器模型(十)

    寄存器有前门和后门两种访问方式,这儿只看后门访问方式 //----------------------------------------------------------------------- ...

  5. cocos 2d-x 3.0配制环境

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...

  6. (一)我的Javascript系列:Javascript的面向对象旅程(上)

    今宵酒醒何处,杨柳岸,晓风残月 导引 我的JavaScript系列文章是我自己对JavaScript语言的感悟所撰写的系列文章.现在还没有写完.目前一共出了下面的系列: (三)我的JavaScript ...

  7. C# 向服务器上传文件(客服端winform、服务端web)

    转载 首先写客服端,winform模拟一个post提交: /// <summary> /// 将本地文件上传到指定的服务器(HttpWebRequest方法) /// </summa ...

  8. mdns小结

    mdns的功能和普通DNS很类似,即提供主机名到IP地址的解析服务.   mdns一些基本特性: 1,mdns主要为小型私有网络(不存在DNS)提供名称解析. 2,mdns使用多播(Multicast ...

  9. UVA208 Firetruck 消防车(并查集,dfs)

    要输出所有路径,又要字典序,dfs最适合了,用并查集判断1和目的地是否连通即可 #include<bits/stdc++.h> using namespace std; ; int p[m ...

  10. [tensorflow] tf2.0 简单例子

    tf2.0笔记 感觉,都统一了,pytorch tensorflow mxnet,大家都差不多了 gan例子笔记 import tensorflow as tf from tensorflow.ker ...