一、链表翻转(leetcode206)

就地逆置法

class Solution {
public:ListNode* reverseList(ListNode* head) {ListNode* next = NULL;ListNode* temp = NULL;while(head){next = head->next;head->next = temp;temp = head;head = next;}return temp;}
};

头插法

#include <iostream>using namespace std;struct listnode
{int value;listnode *next;listnode( int x): value(x), next(NULL) {}
};int main()
{listnode a(10);listnode b(20);listnode c(30);listnode d(40);listnode e(50);a.next = &b;b.next = &c;c.next = &d;d.next = &e;listnode *head = &a;listnode new_node(0); listnode *next = NULL;while(head){next = head->next;head->next = new_node.next;new_node.next = head;head = next;}listnode *new_head = new_node.next;while(new_head){cout<<new_head->value<<" ";new_head = new_head->next;}cout<<endl;return 0;
}

这两种方法其实没什么差别

二、链表中间段逆序(leetcodde92)

class Solution {
public:ListNode* reverseBetween(ListNode* head, int m, int n) {int len = n - m + 1;ListNode* pre_node = NULL;ListNode* pnode = head;while(head && --m){pre_node = head;head = head->next;}ListNode* list_tail = head;ListNode* new_head = NULL;ListNode* next = NULL;while(head && len--){next = head->next;head->next = new_head;new_head = head;head = next;}list_tail->next = head;if(pre_node){pre_node->next = new_head;return pnode;}elsereturn new_head;}
};

三、求公共节点(leetcode160)

//方法一
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {std::set<ListNode*> node_set;while(headA){node_set.insert(headA);headA = headA->next;}while(headB){if(node_set.find(headB) != node_set.end())return headB;headB = headB->next;}return NULL;}
};
//方法二
int get_len(ListNode* head){int len = 0;while(head){len++;head = head->next;}return len;}ListNode* forward( int blen, int slen, ListNode* head){int len = blen - slen;while(head && len){head = head->next;len--;}return head;}
class Solution {
public:ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {int lenA = get_len(headA);int lenB = get_len(headB);if( lenA > lenB){headA = forward(lenA, lenB, headA);}else{headB = forward(lenB, lenA, headB);}while(headB && headA){if(headB == headA){return headB;}headA = headA->next;headB = headB->next;}return NULL;}
};

四、合并有序链表(leetcode21)

class Solution {
public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {ListNode temp_node(0);ListNode* pre;pre = &temp_node;while(l1 && l2){if(l1->val < l2->val){pre->next = l1;l1 = l1->next;}else{pre->next = l2;l2 = l2->next;}pre = pre->next;}if(l1)pre->next = l1;if(l2)pre->next = l2;return temp_node.next;}
};

Leet code链表相关题目初探相关推荐

  1. LeetCode 链表相关题目总结

    之前已经对链表相关常用操作进行了总结.在这里,对 LeetCode 与链表相关的题目解答思路进行总结. 1. 两个链表,求是否有相交的节点并返回 方案一:将两个链表的各节点依次存入两个对象中,然后双层 ...

  2. python链表及其相关题目(更新中)

    链表 1. 链表简介 1.1 定义及相关原理: 1.2 链表的几种特殊形式 1.2.1 双向链表 1.2.2 循环链表 1.3 链表的基本操作 1.3.1 链表的结构定义 1.3.2 建立一个链表 1 ...

  3. 链表类型题目需要用到的头文件list.h

    下面是后面链表相关题目中需要用到的链表结点的定义和相关操作函数,参见下面的list.h文件: 注意链表结点的定义采用cpp的定义方式,它会被cpp的文件调用.比如后面删除链表重复结点的文件del_re ...

  4. leet code: Two Sum

    leet code: Two Sum 题目 Given an array of integers, return indices of the two numbers such that they a ...

  5. Leet Code 力扣 - - 最短最优雅python解法带解析汇总

    Leet Code 刷题笔记 - - 不求最快最省,但求最短最优雅 前言 代码精炼是 Python 的核心,同时能够反应对于语言的熟练程度,本项目目的在于汇总 leet code 最短最优雅的解法,拒 ...

  6. Leet Code题解 - 1559. Detect Cycles in 2D Grid 检测二维无向图中的环

    Leet Code题解 -- 1559. Detect Cycles in 2D Grid 检测二维无向图中的环 前言 一.题目描述 二.思路整理 1. 审题 2. 分布实现步骤 2.1 将二维数组处 ...

  7. 链表相关的面试题型总结

    链表相关的面试题型总结及其个别实现 对指针的掌握程度,是衡量一个程序员的基本功是否扎实的重要考量标准.而数据结构中的链表.二叉树等基本数据结构,是考核指针的最佳利器.本文稍微总结了下链表的主要考点,以 ...

  8. Algorithm:C++语言实现之链表相关算法(链表相加、链表的部分翻转、链表划分、链表去重、重复元素全部删除)

    Algorithm:C++语言实现之链表相关算法(链表相加.链表的部分翻转.链表划分.链表去重.重复元素全部删除) 目录 一.链表 1.1.链表相加 1.2.链表相加 2.1.链表的部分翻转 2.2. ...

  9. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

最新文章

  1. IT人请注意你的身体![转]
  2. 人工智能第一股“旷视科技”赴港递交IPO材料 半年亏损52亿估值280亿
  3. [转]Outlook HTML渲染
  4. 数字图像处理:第二十章 视频编码与压缩
  5. PM 后台配置TCODE
  6. Java-gt;Android并发编程引气入门篇
  7. js将百度坐标转为wgs84
  8. python逐行读取数据时出现错误_Python利用逐行读取readline()打印出现空行的解决办法...
  9. 洛谷 题解 P1135 【奇怪的电梯】
  10. crfpp python
  11. python 中time, datetime的用法
  12. python笔记1-准确掌握列表和元组
  13. paip..net VS2010提示当前上下文中不存在名称的解决
  14. 如何用 latex 排版日文 (xelatex)
  15. Photoshop脚本 设置前景色和背景色
  16. 基于python的学生信息管理系统文献综述_学生信息管理系统的文献综述
  17. java新手学习感想
  18. Computer Graphics Through OpenGL From Theory to Experiments - 学习笔记2 Tricks of the Trade opengl基础
  19. leetcode报错:member access within null pointer of type struct ListNode
  20. 《拐点》简单选股绝招

热门文章

  1. 同步IO(阻塞IO、非阻塞IO), 异步IO的理解
  2. esp8266 AMR转PCM
  3. Microsoft Excel 教程:如何在 Excel 单元格中设置文本格式?
  4. 2017GYL创业营暨青年大会全球招募中
  5. 罗振宇向左,吴晓波向右
  6. Ubuntu22.04虚拟机配置及使用代理工具
  7. ros操作命令与实操-话题发布
  8. 云计算的模型都有哪些
  9. boost spirit ——编译器,语法解析器
  10. Android音频开发之OpenSL ES