• 删除链表中的节点

/**

* Definition for singly-linked list.

* public class ListNode {

* int val;

* ListNode next;

* ListNode(int x) { val = x; }

* }

*/

class Solution {

public void deleteNode(ListNode node) {

node.val=node.next.val;

node.next=node.next.next;

}

}

  • 删除链表的倒数第N个节点

  • 两次遍历算法

    class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {

    ListNode dummy=new ListNode(0);

    dummy.next=head;

    int length=0;

    ListNode p=head;

    while (p!=null) {

    length++;

    p=p.next;

    }

    ListNode first=dummy;

    length-=n;

    while (length>0) {

    length--;

    first=first.next;

    }

    first.next=first.next.next;

    return dummy.next;

    }

    }

  • 一次遍历算法

    class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {

    ListNode dummy = new ListNode(0);

    dummy.next = head;

    ListNode first = dummy;

    ListNode second = dummy;

    // Advances first pointer so that the gap between first and second is n nodes apart

    for (int i = 1; i <= n + 1; i++) {

    first = first.next;

    }

    // Move first to the end, maintaining the gap

    while (first != null) {

    first = first.next;

    second = second.next;

    }

    second.next = second.next.next;

    return dummy.next;

    }

    }

  • 反转链表

  • 我的解法:利用Linklist比较好理解,有点投机取巧,面试不一定可以得吧!!!哈哈哈,但是测试通过了

    class Solution {

    public ListNode reverseList(ListNode head) {

    LinkedList<Integer> list=new LinkedList<>();

    for(ListNode x=head;x!=null;x=x.next) {

    list.add(x.val);

    }

    for(ListNode x=head;x!=null;x=x.next) {

    x.val=list.removeLast();

    }

    return head;

    }

    }

  • 迭代法,一开始不好理解,仔细琢磨后还是很好理解的

    class Solution {

    public ListNode reverseList(ListNode head) {

    if (head==null||head.next==null) {

    return head;

    }

    ListNode newHead=null;

    while (head!=null) {

    ListNode temp=head.next;//先把后面的数据储存到temp

    head.next=newHead;//把head添加到newHead

    newHead=head;//把head添加到newHead,newHead重新作为新的头指针

    head=temp;//让头指针指向下一个数据,已保存在temp里

    }

    return newHead;

    }

    }

  • 递归法,仔细研究后还是可以理解的

    class Solution {

    public ListNode reverseList(ListNode head) {

    return newHead(head);

    }

    public ListNode newHead(ListNode head) {

    if (head==null||head.next==null) {

    return head;

    }

    ListNode newHead=newHead(head.next);

    head.next.next=head;//反转链表,把把下一个数连接到newHead上

    head.next=null;//指向空,防止再次递归导致覆盖后面的内容。

    return newHead;

    }

    }

以上两种方法,通过从网站学习理解,必须站在巨人的肩膀上,哈哈哈!

注明学习网址:https://blog.csdn.net/fx677588/article/details/72357389,虽然不是转载,但有借鉴。

  • 合并两个有序链表

  • 我的解法:很好实现,思路简单,但耗时长一点

    class Solution {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

    LinkedList<Integer> list=new LinkedList<>();

    for(ListNode x=l1;x!=null;x=x.next)

    {

    list.add(x.val);

    }

    for(ListNode x=l2;x!=null;x=x.next) {

    list.add(x.val);

    }

    Collections.sort(list);

    ListNode head=null;

    for (int i = list.size()-1; i>=0; i--) {

    ListNode n=new ListNode(list.get(i));

    n.next=head;

    head=n;

    }

    return head;

    }

    }

  • 大神一解法,递归解法,代码简洁

    class Solution {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

    ListNode rspHead=new ListNode(0);

    ListNode rsp=rspHead;

    while(l1!=null && l2!=null){

    if(l1.val<l2.val){

    rspHead.next=l1;

    rspHead=rspHead.next;

    l1=l1.next;

    }else{

    rspHead.next=l2;

    rspHead=rspHead.next;

    l2=l2.next;

    }

    }

    if(l1==null){

    rspHead.next=l2;

    }else{

    rspHead.next=l1;

    }

    return rsp.next;

    }

    }

  • 大神二解法,利用双指针,现将带头链表头指针用rsp保存,最后返回rsp.next

    class Solution {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

    ListNode rspHead=new ListNode(0);

    ListNode rsp=rspHead;

    while(l1!=null && l2!=null){

    if(l1.val<l2.val){

    rspHead.next=l1;

    rspHead=rspHead.next;

    l1=l1.next;

    }else{

    rspHead.next=l2;

    rspHead=rspHead.next;

    l2=l2.next;

    }

    }

    if(l1==null){

    rspHead.next=l2;

    }else{

    rspHead.next=l1;

    }

    return rsp.next;

    }

    }

  • 回文链表

  • 我的解法:感觉已经脱离了链表,不行,不好,时间太长!

    class Solution {

    public boolean isPalindrome(ListNode head) {

    if (head==null||head.next==null) {

    return true;

    }

    LinkedList<Integer> list=new LinkedList<>();

    for(ListNode x=head;x!=null;x=x.next) {

    list.add(x.val);

    }

    for (int i = 0; i < list.size()/2; i++) {

    if (list.get(i).compareTo(list.get(list.size()-i-1))!=0){

    return false;

    }

    }

    return true;

    }

    }

  • 大神解法:挺好!先找链表中点,然后再反转后半部分,最后再分别遍历比较,直到后半部分遍历完!

    class Solution {

    ListNode node1=new ListNode(0);

    ListNode node2=new ListNode(0);

    public boolean isPalindrome(ListNode head) {

    if (head==null||head.next==null) {

    return true;

    }

    ListNode slow=head;

    ListNode fast=head;

    while (fast.next!=null&&fast.next.next!=null) {

    fast=fast.next.next;

    slow=slow.next;

    }

    slow=reverse(slow.next);//此处如果是show的话,将从中点的前一个匹配,比较难理解,后面附图说明

    while (slow!=null) {

    if (head.val!=slow.val) {//到这里,head短的话会成为空指针而报错!!!

    return false;

    }

    head=head.next;

    slow=slow.next;

    }

    return true;

    }

    private ListNode reverse(ListNode head) {

    if (head==null||head.next==null) {

    return head;

    }

    ListNode newHead=reverse(head.next);

    head.next.next=head;

    head.next=null;

    return newHead;

    }

    }

说明:如果测试样例输入0,0的话,黄色标记为什么会报错!

  • 环形链表

  • 我的解法:利用hashset

    class Solution {

    public boolean hasCycle(ListNode head) {

    if (head==null||head.next==null) {

    return false;

    }

    Set<ListNode> set=new HashSet<>();

    int n=0;

    ListNode x=head;

    while (set.size()>=n) {

    set.add(x);

    x=x.next;

    n++;

    if (x==null) {

    return false;

    }

    }

    return true;

    }

    }

  • 大神解法:

    class Solution {

    public boolean hasCycle(ListNode head) {

    if (head==null) {

    return false;

    }

    ListNode l1=head,l2=head.next;

    while (l1!=null&&l2!=null&&l2.next!=null) {

    l1=l1.next;

    l2=l2.next.next;

    if (l1==l2) {

    return true;

    }

    }

    return false;

    }

    }

  • 递归解法,更为简洁

    class Solution {

    public boolean hasCycle(ListNode head) {

    if(head==null||head.next==null)return false;

    if(head.next==head)return true;

    ListNode l=head.next;

    head.next=head;

    boolean isCycle=hasCycle(l);

    return isCycle;

    }

    }

算法练习LeetCode初级算法之链表的更多相关文章

  1. 【LeetCode算法】LeetCode初级算法——字符串

      在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...

  2. 算法练习LeetCode初级算法之字符串

    反转字符串 我的解法比较low,利用集合的工具类Collections.reverse反转,用时过长 class Solution { public void reverseString(char[] ...

  3. 算法练习LeetCode初级算法之数组

    删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数     官方解答:  同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ...

  4. 算法练习LeetCode初级算法之其他

    位1的个数 解法一: class Solution { // you need to treat n as an unsigned value public int hammingWeight(int ...

  5. 算法练习LeetCode初级算法之数学

    Fizz Buzz class Solution { public List<String> fizzBuzz(int n) { List<String> list=new L ...

  6. 算法练习LeetCode初级算法之设计问题

    打乱数组 不断的让第一个与后面随机选择的数交换 class Solution { private int[] nums; private int[] initnums; public Solution ...

  7. 算法练习LeetCode初级算法之动态规划

    爬楼梯:斐波那契数列 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 非递归解法 class S ...

  8. 算法练习LeetCode初级算法之排序和搜索

    合并两个有序数组 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arrayco ...

  9. 算法练习LeetCode初级算法之树

    二叉树的前序遍历 我的解法:利用递归,自底向下逐步添加到list,返回最终的前序遍历list class Solution { public List<Integer> preorderT ...

随机推荐

  1. EasyUI datagrid 序列 化后时间 处理 九

    @{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...

  2. 2019西湖论剑网络安全技能大赛(大学生组)部分WriteUp

    这次比赛是我参加以来成绩最好的一次,这离不开我们的小团队中任何一个人的努力,熬了一整天才答完题,差点饿死在工作室(门卫大爷出去散步,把大门锁了出不去,还好学弟提了几个盒饭用网线从窗户钓上来才吃到了午饭 ...

  3. mac 中host设置方法

    在开发中,有的接口为了安全考虑,只能通过指定的域名去反问,这时本地启动的 localhost 就无法获取到数据,需要去更改电脑的host文件配置,下面介绍mac 电脑的设置方法 1. 打开终端,输入一 ...

  4. [转]C#程序性能优化

    C#程序性能优化 1.显式注册的EvenHandler要显式注销以避免内存泄漏 将一个成员方法注册到某个对象的事件会造成后者持有前者的引用.在事件注销之前,前者不会被垃圾回收.   private v ...

  5. java File处理

    /**************************************************************************************工具类********** ...

  6. Consul之:服务注册与发现

    一.服务的管理(注册与发现)有三种方式: 1:通过配置文件的方式静态注册2:通过HTTP API接口来动态注册(spring cloud使用方式,spring cloud中使用的是consul api ...

  7. TensorFlow 安装报错的解决办法(安装1.5版本)

    1.安装Anaconda 百度下载windows版本,一路点下一步,安装好了Anaconda,自带python3.6.6. 2.安装TensorFlow (1)打开Anaconda Prompt,输入 ...

  8. 网络共享存储服务NFS

    网络共享存储服务NFS 作者:Eric 微信:loveoracle11g 环境准备 服务器系统 角色 IP RHEL 7.5 x86-64 NFS服务端 192.168.10.201 RHEL 7.5 ...

  9. 8-安装Kafka

    1.解压 tar -zxvf kafka_2.11-0.9.0.1.tar -C /opt/app/ 2.改权限 chown -R hadoop:hadoop /opt/app/ 3.修改配置文件 c ...

  10. oracle之分析函数解析及其应用场景

    ORACLE 分析函数FIRST_VALUE,LAST_VALUE用法sum overavg over first_value overlast_value over...聚合函数结合over就是分析 ...