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. .NET Core RSA 指南与增强扩展 RSAExtensions

    一. 前言 RSA 作为最常用的非对称加密算法,在我们的实际使用中还是比较常见的,特别是对接支付十有八九都会遇到,或者是其他需要数据安全的业务场景.在 .NET Framework 以及 .NET C ...

  2. firefox浏览器写xpath

    最近在学xpath发现Firefox浏览器不支持xpath定位页面元素 百度为例: F12 页面前端代码  输入最简单的xpath发现并不能定位元素 解决方案:添加 Try Xpath 这个插件,因为 ...

  3. Linux Bash之通配符

    通配符是我们在shell环境中不知不觉中都会用到的,有时甚至都不会考虑到去探究其实现过程,因为使用得太普遍了.而清晰地理解每一个过程,将有助于我们的分析和调试. 说白了,通配符就是在 shell 环境 ...

  4. HttpRunner学习6--使用parameters参数化

    前言 在使用HttpRunner测试过程中,我们可能会遇到这种场景: 账号登录功能,需要输入用户名和密码,设计测试用例后有 N 种组合情况 如果测试组合比较少,比如只有2个,那我们直接在YAML脚本中 ...

  5. python 多线程编程之_thread模块

    参考书籍:python核心编程 _thread模块除了可以派生线程外,还提供了基本的同步数据结构,又称为锁对象(lock object,也叫原语锁.简单锁.互斥锁.互斥和二进制信号量). 下面是常用的 ...

  6. 如何编写一个TS程序?

    第一步:我们首先需要个代码编辑器-VSCode  点击此处下载(你会下载到rar文件) 第二步:我们还需要下载NodeJS,因为这里有npm,npm是包管理工具,可以下载TypeScript. 注意: ...

  7. 【iOS翻译】对UIGestureRecognizer多种手势傻傻分不清

    UIGestureRecognizerDelegate A set of methods implemented by the delegate of a gesture recognizer to ...

  8. ios学习之路:Xcode+swift+打包ipa一步一坑记录

    咳咳,作为公司的Android开发(兼java接口开发,兼软件测试,兼运维……)由于公司ios开发小伙伴离我而去,ios的app出了问题,急需处理.于是领导决定由我来处理一下.就是用证书重新打包的事儿 ...

  9. SpringMVC使用Redis共享session

    在使用之前,请确认项目已经整合了Redis 一.加入依赖 <dependency> <groupId>org.springframework.session</group ...

  10. 使用ReentrantLock

    /** * java.util.concurrent.locks包提供的ReentrantLock用于替代synchronized加锁* 因为synchronized是Java语言层面提供的语法,所以 ...