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. variable fonts - 更小更灵活的字体

    原文链接 variable fonts(下文中vf为缩写)是数字时代制作的字体技术,用更小的文件大小在web上提供更丰富的排版,但是一项新的技术往往伴随着新的挑战和复杂未知的情况.不过,我们要拥抱技术 ...

  2. Java中跳出多重嵌套循环的方法

    一.使用标号 1.多重嵌套循环前定义一个标号 2.里层循环的代码中使用带有标号 break 的语句 public static void main(String[] args) { ok: for(i ...

  3. LeetCode--300. 最长递增子序列

    题目:给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7,101,18] 输出: 4 解释: 最长的上升子序列是 [2,3,7,101],它的长度是 4 ...

  4. 大数据学习笔记——Java篇之集合框架(ArrayList)

    Java集合框架学习笔记 1. Java集合框架中各接口或子类的继承以及实现关系图: 2. 数组和集合类的区别整理: 数组: 1. 长度是固定的 2. 既可以存放基本数据类型又可以存放引用数据类型 3 ...

  5. Vue中使用iconfont

    学习博客:https://www.imooc.com/article/33597?block_id=tuijian_wz

  6. JS-选择排序

    选择排序 选择排序的原理如下.遍历数组,设置最小值的索引为 0,如果取出的值比当前最小值小,就替换最小值索引,遍历完成后,将第一个元素和最小值索引上的值交换.如上操作后,第一个元素就是数组中的最小值, ...

  7. 导入做好的java项目出现下面的错误:The project cannot be built until build path errors are resolved

    例子:       作者在eclipse中导入一个新的项目时,出现了三个错误,如图1中所示: 图1 3 errors 原因分析:        在这个工程中,作者在写的时候,在build path中添 ...

  8. CCF-CSP题解 201712-3 Crontab

    做完一定要仔仔细细地看一遍题目再交,之后发现坑点只能追悔莫及.比如这次"英文缩写(不区分大小写)"\(OwQ\). 给定多个周期性执行的任务,每个任务调度执行有时间的要求.求给定时 ...

  9. 微服务架构 SpringBoot(一)

    spring Boot:官网地址 https://spring.io/ 由来: 随着spring组件功能的强大,配置文件也越来越复杂繁琐,背离了spring公司的简洁快速开发原理,2015年就推出Sp ...

  10. Linux命令-grep,sed,awk

    grep (global search regular expression[RE] and print out the line) 正则表达式全局搜索并将行打印出来 在文件中查找包含字符串" ...