1. 206. 反转链表(易)

    反转一个单链表。

    示例:

    输入: 1->2->3->4->5->NULL
    输出: 5->4->3->2->1->NULL

    c++:

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode* reverseList(ListNode* head) {
    ListNode *newHead=NULL;
    while(head){
    ListNode *next=head->next;
    head->next=newHead;
    newHead=head;
    head=next;
    }
    return newHead;
    }
    };

    python:

    # Definition for singly-linked list.
    # class ListNode:
    # def __init__(self, x):
    # self.val = x
    # self.next = None class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
    newHead=None
    while head!=None:
    nxt=head.next
    head.next=newHead
    newHead=head
    head=nxt
    return newHead

    java:

    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode reverseList(ListNode head) {
    ListNode newHead=null;
    while(head!=null){
    ListNode next=head.next;
    head.next=newHead;
    newHead=head;
    head=next;
    }
    return newHead;
    }
    }
  2. 92. 反转链表 II(中)反转从位置 mn 的链表。请使用一趟扫描完成反转。
    说明:
    1 ≤ m ≤ n ≤ 链表长度。
    示例:
    输入: 1->2->3->4->5->NULL, m = 2, n = 4
    输出: 1->4->3->2->5->NULL
    c++:

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
    ListNode* sta=head;
    int len=n-m+1;
    ListNode* pre=NULL;
    while(--m&&head){
    pre=head;
    head=head->next;
    }
    ListNode* e=head;
    ListNode* newHead=NULL;
    while(len--&&head){
    ListNode* next=head->next;
    head->next=newHead;
    newHead=head;
    head=next;
    }
    e->next=head;
    if(pre)
    pre->next=newHead;
    else
    sta=newHead;
    return sta;
    }
    };

    python:

    # Definition for singly-linked list.
    # class ListNode:
    # def __init__(self, x):
    # self.val = x
    # self.next = None class Solution:
    def reverseBetween(self, head: ListNode, m: int, n: int) -> ListNode:
    sta=head
    len = n-m+1
    pre=None
    while m!=1 and head!=None :
    pre=head
    m-=1
    head=head.next
    newHead=None
    ed=head
    while len!=0 and head!=None:
    nxt=head.next
    head.next=newHead
    newHead=head
    head=nxt
    len-=1
    ed.next=head
    if pre!=None:
    pre.next=newHead
    else :
    sta=newHead
    return sta

    java:

    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
    int len=n-m+1;
    ListNode pre=null,sta=head;
    while(--m!=0&&head!=null){
    pre=head;
    head=head.next;
    }
    ListNode ed=head,newHead=null,nxt=null;
    while(len--!=0&&head!=null){
    nxt=head.next;
    head.next=newHead;
    newHead=head;
    head=nxt;
    }
    ed.next=head;
    if(pre==null){
    return newHead;
    }else {
    pre.next=newHead;
    return sta;
    }
    }
    }
  3. 160. 相交链表 (易)

    编写一个程序,找到两个单链表相交的起始节点。

    如下面的两个链表

    在节点 c1 开始相交。
    c++:

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
    int a=0,b=0;
    ListNode *A=headA,*B=headB;
    while(A){
    a++;
    A=A->next;
    }
    while(B){
    b++;
    B=B->next;
    }
    if(a>b){
    int c=a-b;
    while(c--&&headA){
    headA=headA->next;
    }
    while(headA&&headB){
    if(headA==headB)
    return headA;
    else
    headA=headA->next,headB=headB->next;
    }
    }else {
    int c=b-a;
    while(c--&&headB){
    headB=headB->next;
    }
    while(headA&&headB){
    if(headA==headB)
    return headA;
    else
    headA=headA->next,headB=headB->next;
    }
    }
    return NULL;
    }
    };
  4. 142. 环形链表 II(中)

    给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
    为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
    说明:不允许修改给定的链表。
    c++:
    map直接搞:

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode *detectCycle(ListNode *head) {
    unordered_map<ListNode*,int> m;
    while(head){
    if(m[head]){
    return head;
    }
    m[head]=1;
    head=head->next;
    }
    return NULL;
    }
    };

    快慢指针:

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode *next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode *detectCycle(ListNode *head) {
    ListNode *slow=head,*fast=head,*meet=NULL;
    while(slow&&fast){
    slow=slow->next;
    fast=fast->next;
    if(!fast){
    return NULL;
    }
    fast=fast->next;
    if(slow==fast){
    // printf("%d %d",slow->val,fast->val);
    // puts("gg");
    meet=slow;
    break;
    }
    }
    // printf("hh");
    if(meet==NULL)
    return NULL;
    // printf("hh");
    while(meet&&head){
    if(meet==head){
    return meet;
    }
    meet=meet->next;
    head=head->next;
    }
    return NULL;
    }
    };
  5. 2. 两数相加(中)

    给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

    如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

    您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

    示例:

    输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
    输出:7 -> 0 -> 8
    原因:342 + 465 = 807

    思路:题意要理解清楚,模拟即可,很简单但要注意细节。

    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode t=new ListNode(0);
    ListNode cur=t;
    int pre=0;
    while(l1!=null||l2!=null){
    int x=(l1==null?0:l1.val);
    int y=(l2==null?0:l2.val);
    int sum=(x+y+pre);
    cur.next=new ListNode(sum%10);
    cur=cur.next;
    pre=sum/10;
    if(l1!=null)
    l1=l1.next;
    if(l2!=null)
    l2=l2.next;
    }
    if(pre!=0){
    cur.next=new ListNode(pre);
    }
    return t.next;
    }
    }

      

     

leetcode——链表的更多相关文章

  1. [LeetCode] [链表] 相关题目总结

    刷完了LeetCode链表相关的经典题目,总结一下用到的技巧: 技巧 哑节点--哑节点可以将很多特殊case(比如:NULL或者单节点问题)转化为一般case进行统一处理,这样代码实现更加简洁,优雅 ...

  2. Leetcode链表

    Leetcode链表 一.闲聊 边学边刷的--慢慢写慢慢更 二.题目 1.移除链表元素 题干: 思路: 删除链表节点,就多了一个判断等值. 由于是单向链表,所以要删除节点时要找到目标节点的上一个节点, ...

  3. [LeetCode] 链表反转相关题目

    暂时接触到LeetCode上与链表反转相关的题目一共有3道,在这篇博文里面总结一下.首先要讲一下我一开始思考的误区:链表的反转,不是改变节点的位置,而是改变每一个节点next指针的指向. 下面直接看看 ...

  4. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

  5. LeetCode链表相加-Python<二>

    上一篇:LeetCode两数之和-Python<一> 题目:https://leetcode-cn.com/problems/add-two-numbers/description/ 给定 ...

  6. leetcode 链表类型题总结

    链表测试框架示例: // leetcodeList.cpp : 定义控制台应用程序的入口点.vs2013 测试通过 // #include "stdafx.h" #include ...

  7. leetcode链表相关

    目录 2/445两数相加 综合题(328奇偶链表, 206反转链表, 21合并两个有序链表 ) 92反转链表 II 链表排序(148排序链表, 876链表的中间结点) 142环形链表 II 160相交 ...

  8. LeetCode 链表题 ( Java )

    leetcode 237. 删除链表中的节点 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/ 示例 : 输入: he ...

  9. LeetCode 链表的插入排序

    Sort a linked list using insertion sort 创建一个新的链表,将旧链表的节点插入到正确的位置 package cn.edu.algorithm.huawei; pu ...

  10. leetcode 链表类型题目解题总结

    最基础的方式要做到非常熟练,要熟练到不思考就能写,但又需明白各处的要求和陷阱 合并两个有序链表的操作,在前面加上一个初始节点,注意while循环和退出时的处理,理解如何处理其中一个链表遍历完的情况 L ...

随机推荐

  1. Appium之选择/操作元素

    Appium是如何选择.操作元素的呢? appium自动化  ------  选择界面 元素 操作元素  ------- ① 点击 ② 输入字符 ③ 拖拽 ④ 获取页面元素的各种属性 根据appium ...

  2. Django 07

    目录 ORM查询优化 only与defer(单表) select_related与prefetch_related(跨表) choices参数 MTV与MVC模型 Ajax简介 前后端传输数据编码格式 ...

  3. Vue AES+MD5加密 后台解密

    前端VUE vue项目中安装crypto-js npm install crypto-js --save-dev CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加 ...

  4. JS-scrollTop、scrollHeight、clientTop、clientHeight、offsetTop、offsetHeight的理解

    scrollTop, 可写(这些属性中唯一一个可写的元素) Element.scrollTop 属性可以获取或设置一个元素的内容垂直滚动的像素数. 一个元素的 scrollTop 值是这个元素的顶部到 ...

  5. Mybatis需要注意的细节

    mybatis第二篇 1.${}和#{}的区别 1.#在传参的时候,会自动拼接单引号:$不能拼接单引号; 2.$传参时,一般不支持jdbcType指定类型的写法;#则可以;如: ​ #{name,jd ...

  6. Vmare 无法打开内核设备“\\.\VMCIDev\VMX”: 系统找不到指定的文件。您在安装 VMware Workstation 后是否进行了重新引导?的解决办法

    1.使用管理员省份运行cmd:net start vmx86(切记是要用管理员身份),启动服务成功问题即可解决. 2.若1操作中启动失败,则到Vmare安装目录下搜索vmx86.sys文件,并拷贝到C ...

  7. NodeJS3-2基础API----Buffer(缓冲器)

    Buffer(缓冲器) Buffer是用于处理二进制数据流的 实例类似整数数组,大小固定(实例化之后,是多大就多大,不能进行变更) C++代码在V8 对外分配物理内存 Buffer是全局变量,没必要使 ...

  8. 转自自己的关于落谷计数器【p1239】的题解

    本蒟蒻写这道题用了两天半里大概五六个小时.(我太弱了) 然后这篇题解将写写我经历的沟沟坎坎,详细的分析一下, 但是由于它很长,因此一定还有多余的地方,比如说我的 预处理,可能比较多余.但是我觉得,信息 ...

  9. SpringBoot2基础,进阶,数据库,中间件等系列文章目录分类

    本文源码:GitHub·点这里 || GitEE·点这里 一.文章分类 1.入门基础 SpringBoot2:环境搭建和RestFul风格接口 2.日志管理 SpringBoot2:配置Log4j2, ...

  10. Python提升“技术逼格”的6个方法

    1 列表生成式和生成器 from numpy import randoma = random.random(10000) lst = []for i in a: lst.append(i * i) # ...