题目描述

Given a singly linked list L: L 0→L 1→…→L n-1→L n,
reorder it to: L 0→L nL 1→L n-1→L 2→L n-2→…

You must do this in-place without altering the nodes' values.

For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.

public class Solution {public void reorderList(ListNode head) {if(head == null || head.next == null) {return;}ListNode slow = head;ListNode quick = head;while(quick.next != null && quick.next.next != null) {slow = slow.next;quick = quick.next.next;}ListNode mid = slow.next;if(mid != null && mid.next != null) {ListNode next = mid.next;while(next != null) {mid.next = next.next;next.next = slow.next;slow.next = next;next = mid.next;}}ListNode p = head;ListNode q = slow.next;while(q != null && p != null){slow.next = q.next;q.next = p.next;p.next = q;p = p.next.next;q = slow.next;}}
}

题目描述

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

Follow up:
Can you solve it without using extra space?

public class Solution {public ListNode detectCycle(ListNode head) {if (head != null) {ListNode slow = head;ListNode fast = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {fast = head;while (slow != fast) {slow = slow.next;fast = fast.next;}return slow;}}}return null;}
}

题目描述

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

Follow up:
Can you solve it without using extra space?

public class Solution {public boolean hasCycle(ListNode head) {if(head != null) {ListNode slow = head;ListNode fast = head;while(fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if(fast == slow) {return true;}}}return false;}
}

题目描述

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s ="leetcode",
dict =["leet", "code"].

Return true because"leetcode"can be segmented as"leet code".

public class Solution {public boolean wordBreak(String s, Set<String> dict) {boolean[] temp = new boolean[s.length() + 1];temp[0] = true;for(int i = 1; i < temp.length; i++) {for(int j = 0; j < i; j++) {if (temp[j] && dict.contains(s.substring(j, i))){temp[i] = true;break;}}}return temp[temp.length - 1];}
}

题目描述

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.

Return a deep copy of the list.

public class Solution {public RandomListNode copyRandomList(RandomListNode head) {if(head == null || (head.next == null && head.random == null)){return head;}RandomListNode node = head;while(node != null){RandomListNode copy = new RandomListNode(node.label);copy.next = node.next;copy.random = node.random;node.next = copy;node = node.next.next;}node = head.next;while(node != null){if(node.next != null){node.next = node.next.next;}if(node.random != null){node.random  = node.random.next;}node = node.next;}return head.next;}}

20190730算法题存档相关推荐

  1. 算法题存档20200505

    给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和 ...

  2. 算法题存档20191223

    题目描述 删除给出链表中的重复元素,使链表中的所有元素都只出现一次 例如: 给出的链表为1->1->2,返回1->2. 给出的链表为1->1->2->3->3 ...

  3. 算法题存档20190207

    题目描述 如果一个整数只能被1和自己整除,就称这个数是素数. 如果一个数正着反着都是一样,就称为这个数是回文数.例如:6, 66, 606, 6666 如果一个数字既是素数也是回文数,就称这个数是回文 ...

  4. 算法题存档20190127

    题目描述 假设一个探险家被困在了地底的迷宫之中,要从当前位置开始找到一条通往迷宫出口的路径.迷宫可以用一个二维矩阵组成,有的部分是墙,有的部分是路.迷宫之中有的路上还有门,每扇门都在迷宫的某个地方有与 ...

  5. 算法题存档20200627(树)

    给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表. 如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 Tru ...

  6. 算法题存档2020425

    给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [   [1,2,3],   [1,3,2],   [2,1,3],   [2,3,1],   [3, ...

  7. 20191219算法题存档

    题目描述 给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组 注意: 可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和n public class ...

  8. 20190724算法题存档

    题目描述 Sort a linked list in O(n log n) time using constant space complexity. public class Solution {p ...

  9. 20190719算法题存档

    题目描述 小Q得到一个神奇的数列: 1, 12, 123,...12345678910,1234567891011.... 并且小Q对于能否被3整除这个性质很感兴趣. 小Q现在希望你能帮他计算一下从数 ...

最新文章

  1. 第三次作业+105032014085
  2. 为什么大公司一定要使用DevOps?
  3. select chosen-jquery.js下拉列表的层被覆盖
  4. 2018年中国视频监控行业现状及行业发展趋势分析预测【图】
  5. so baby come on~~
  6. 在CentOS 7上安装使用Kubernetes:管理云平台多个主机上的容器化应用
  7. vue-count-to插件使用方法
  8. const成员函数、inline成员函数、static成员函数中关键字的位置
  9. 如何对CAD图纸快速测量?
  10. MyBatis官网下载步骤
  11. JavaEclipse中从Lambada表达式运行错误的解决方法到学到新知识
  12. MySQL中的删除:drop,delete,truncate的区别和联系
  13. 计算机专业如何自己增加项目经验?
  14. Dcloud学习资料汇总+视频教程
  15. qt网络编程及readyread信号
  16. UIPickerView 用法
  17. 查询Apple app的bundle ID
  18. js控制浏览器全屏显示
  19. DFS(深度优先搜索)学习笔记(C语言版本)
  20. 关于解决2345流氓软件,默认打开2345网址的问题

热门文章

  1. Protobuf生成Java代码(命令行)
  2. VS Code将vue项目上传到github/gitee过程以及报错调试
  3. Spring获取前台参数的几种方式
  4. Java命名规范(建议收藏)
  5. 包含几通道数据_温度采集,无处不测!「数据采集」
  6. 【Redis】redis基本数据结构之ZSet
  7. OSChina 周日乱弹 —— 七哥的北漂日记
  8. Linux运维面试题之--网页打开缓慢如何优化
  9. scikit-learn——快速入门 - daniel-D(转)
  10. Android屏幕禁止休眠的方法