19. Remove Nth Node From End of List

删除倒数第N个结点

解题思路

声明两个指针,快指针先移动n次,然后快慢指针同时移动,直到快指针为null。

此时慢指针指向了倒数第n个结点的前驱结点,然后删除慢指针后继结点即可。

需要注意的是当删除倒数第n(n与链表长度相同)时,快指针会直接移动到null。

此时需要单独处理,直接将head指向head的后继结点即可。

public ListNode RemoveNthFromEnd(ListNode head, int n) {ListNode zero=new ListNode(0);zero.next=head;ListNode fast=zero;ListNode slow=zero;//若链表只有一个结点,直接清空if(head.next==null){head=null;return head;}//将快指针放向前移动n次for(int i=1;i<=n+1;i++)fast=fast.next;//如果快指针已经为空,说明清除的是第一个结点,直接将head指向其后继结点if(fast==null){head=head.next;return head;}//循环遍历链表直到快指针到达尾部while(fast!=null){fast=fast.next;slow=slow.next;}//此时slow的后继结点为要删除的倒数第n个结点slow.next=slow.next.next;return zero.next;
}    

21. Merge Two Sorted Lists

合并两个有序链表

解题思路

使用递归方法调用自身,让结点一直递归到两个链表中其中一个比另一个尾部的结点大的结点。

到最后一次调用自身,会触发两个if中的一个,返回链表剩余的结点。

每次返回链表的首节点都会成为上一个结点的后继结点,不断返回后,就实现了两个链表有序的合并。

public ListNode MergeTwoLists(ListNode l1, ListNode l2) {if(l1==null)return l2;if(l2==null)return l1;ListNode newList=null;//递归调用自身,递归到两链表中最后剩的最大的链表,赋值为前一个小值的nextif(l1.val<=l2.val){newList=l1;newList.next=MergeTwoLists(l1.next,l2);}else{newList=l2;newList.next=MergeTwoLists(l1,l2.next);}return newList;
}

141. Linked List Cycle

检查链表中是否有环

解题思路

使用快慢指针,快指针一次步进慢指针的n倍,如果存在环,二者会不断循环,最终会有追上且二者相等的情况。

如果不存在环,快指针或快指针的后继结点肯定会等于null,返回false。

public bool HasCycle(ListNode head) {//快慢指针检测单链表中是否存在环if(head==null||head.next==null)return false;//快指针一次前进n倍慢指针的步数ListNode fastNode=head.next;ListNode slowNode=head;//如果存在环,二者一定会碰上,得到相等的情况while(fastNode!=slowNode){if(fastNode==null||fastNode.next==null)return false;fastNode=fastNode.next.next;slowNode=slowNode.next;}return true;
}

206. Reverse Linked List

反转链表

解题思路

反转链表的核心就是让等待被提到前面的结点标记为当前结点,让等待别的结点到自己前面的结点称为前驱结点,记录下当前结点的后继结点。

然后将当前结点提到前驱结点前,第一次循环时,链表会断开。然后更新当前结点、前驱结点。

之后的循环每次都是先记录下当前结点的后继结点作为下一次循环的当前结点,再执行提前和更新操作。

public ListNode ReverseList(ListNode head) {if(head==null)return head;ListNode pre=head;ListNode cur=head.next;ListNode temp;while (cur!= null){//第一次循环时将第二个结点提到第一个节点前,此时链表断开。//之后的循环就是不断将后部分的首结点提到前部分的首位。//同时更新pre、cur和temp指针为前部分的首结点、后部分的首结点和后部分首结点的后继结点。 temp=cur.next;//设置后继结点指针cur.next=pre;//将当前结点提到前驱结点前pre=cur;//设置前驱结点指针cur=temp;//设置当前结点指针
        }head.next=null;return pre;
}

707. Design Linked List

设计链表

解题思路

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1

addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。

addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。

addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val  的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。

deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

public class MyLinkedList {//单链表结点类public class Node{public int val;public Node next;//实例构造器public Node(int item){val=item;next=null;}public Node(){val=0;next=null;}}//头结点和数量public Node head;public int count;/** Initialize your data structure here. */public MyLinkedList() {head=null;count=0;}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */public int Get(int index) {//判断索引是否越界if(index>=count)return -1;Node p=head;int num=0;//遍历链表,找到索引指向的结点位置,返回该结点的值while(true){if(num==index)return p.val;num++;p=p.next;}}/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */public void AddAtHead(int val) {//更新headNode p=new Node(val);p.next=head;head=p;count++;}/** Append a node of value val to the last element of the linked list. */public void AddAtTail(int val) {Node p=new Node(val);Node q=head;//遍历链表找到尾部,添加节点while(q.next!=null){q=q.next;}q.next=p;count++;}/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */public void AddAtIndex(int index, int val) {//索引为0直接执行在头结点前添加结点的方法if(index==0){AddAtHead(val);return;}//索引和链表长度相直接执行在尾部添加结点的方法if(index==count){AddAtTail(val);return;}//索引越界直接返回if(index>=count)return;int num=0;Node p=head;//循环遍历,在索引指向结点之前添加结点while(true){num++;if(num==index){Node q=new Node(val);q.next=p.next;p.next=q;count++;return;}p=p.next;}}/** Delete the index-th node in the linked list, if the index is valid. */public void DeleteAtIndex(int index) {//索引越界直接返回if(index>=count)return;int num=0;Node p=head;//遍历链表,删除索引指向的结点while(true){num++;if(num==index){p.next=p.next.next;count--;return;}p=p.next;}}
}/*** Your MyLinkedList object will be instantiated and called as such:* MyLinkedList obj = new MyLinkedList();* int param_1 = obj.Get(index);* obj.AddAtHead(val);* obj.AddAtTail(val);* obj.AddAtIndex(index,val);* obj.DeleteAtIndex(index);*/

876. Middle of the Linked List

获取链表的中间结点

解题思路

获取其中间结点我也是使用了快慢指针,让快慢指针都以后继结点为head出发。

快指针步进为慢指针的二倍,快指针或快指针的后继结点为null就返回慢指针,慢指针即为中间结点。

public ListNode MiddleNode(ListNode head) {ListNode zero = new ListNode(0);zero = head;//快指针和慢指针以head为next开始ListNode fast = zero;ListNode slow = zero;if (head.next == null)return head;while (true){//快慢指针步进fast = fast.next.next;slow = slow.next;//直到快指针为null或快指针的后继结点为null,返回慢指针指向的结点if (fast == null||fast.next == null)return slow;}
}

转载于:https://www.cnblogs.com/errornull/p/9860724.html

LeetCode-C#实现-链表(#19/21/141/206/707/876)相关推荐

  1. 《LeetCode力扣练习》第141题 环形链表 Java

    <LeetCode力扣练习>第141题 环形链表 Java 一.资源 题目: 给你一个链表的头节点 head ,判断链表中是否有环. 如果链表中有某个节点,可以通过连续跟踪 next 指针 ...

  2. leetcode 中的链表简单题 python3

    目录: 基本使用 // 21题 合并有序链表 // 83题 删除有序链表重复元素 // 141题 环形链表 // 160题 相交链表 // 203题 移除链表元素// 206题 反转链表 // 基本使 ...

  3. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  4. 学长冷月带你怒刷LeetCode之反转链表

    本文目录 前言 题目描述 冷月题解 总结一下 前言 链表的操作是数据结构中最基础的算法之一,反转列表也是一道经典的笔试题.很多公司的面试题库中都有这道题,有的公司明确题目要求不能使用额外的节点存储空间 ...

  5. LeetCode 92反转链表Ⅱ93复制ip地址94二叉树的中序遍历

    微信搜一搜:bigsai 专注于Java.数据结构与算法,一起进大厂不迷路! 算法文章题解全部收录在github仓库bigsai-algorithm,求star! 关注回复进群即可加入力扣打卡群,欢迎 ...

  6. LeetCode篇之链表:1290(二进制链表转整数)

    LeetCode篇之链表:1290-->二进制链表转整数 1290. 二进制链表转整数 题目: 解题思路: 源码: 改进: 踩坑点: 1290. 二进制链表转整数 题目: 解题思路: 1.先遍历 ...

  7. 【LeetCode】剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

    [LeetCode]剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 文章目录 [LeetCode]剑指 Offer 21. 调整数组顺序使奇数位于偶数前面 一.双指针 一.双指针 解题思路: ...

  8. LeetCode Python实现 链表简单部分

    LeetCode Python实现 链表简单部分 我以前完全没有写过关于链表的东西,node.val=node.next.val#当前值被后一个值覆盖node.next=node.next.next# ...

  9. LeetCode:旋转链表【61】

    LeetCode:旋转链表[61] 题目描述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5- ...

最新文章

  1. android view设置按钮颜色_Android 主题换肤技术方案分析
  2. 解决vim没有颜色的办法
  3. 李彦宏说自动驾驶比人更安全,还认为中国用户更愿意放弃隐私
  4. 探究:如何判断Delphi中的对象指针是否可用
  5. 用python查询数据库_用python 做数据库查询
  6. 漫画:程序员每天的6场战斗
  7. 传智播客python2018_Python视频教程下载-2018传智播客Python视频教程下载-西西软件下载...
  8. 【干货】从数字化洞察新消费趋势看数字化如何赋能企业.pdf(附下载链接)
  9. 利用卷积自编码器对图片进行降噪
  10. NVIDIA驱动重装经历
  11. windows搭建nginx + php环境
  12. 说说自己找互联网工作的经验
  13. LaTex数学之积分、求和和极限
  14. Windows10 安装spyder
  15. html datatable修改行,DataTable根据条件修改行颜色和特定格颜色
  16. 计算机硬盘储存怎么增加,如何扩大存储空间?电脑扩大新添加的硬盘的方法
  17. 【MATLAB100个实用小技巧】——界面设计(33-43)
  18. [Android UI界面] Android UI 设计准则
  19. Linux --指令 (四) rm 和 rmdir
  20. 投资理财-言微不劝人

热门文章

  1. 为什么不能生成accde_原来“转换生成语言学”就是这么回事
  2. python 乘法内置函数_Python内置函数--reversed()
  3. python与人工智能的关系_广东江门的北大青鸟学校了解到人工智能与Python关系
  4. WPF RadioButton按钮控件取消选中设置
  5. ZOJ 3988 2017CCPC秦皇岛 H:Prime Set(二分匹配)
  6. Prometheus 原理和实践,含docker部署Prometheus、node Exporters、Alertmanager、Push Gateway、grafana
  7. matlab2c使用c++实现matlab函数系列教程-abs函数
  8. zedboard连接pmod oledrgb
  9. java 简单数组_Java 数组分析及简单实例
  10. [BZOJ1815BZOJ1488]有色图/图的同构(Polya定理)