表不支持随机查找,通常是使用next指针进行操作。

206. 反转链表

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//时间:O(n) 只遍历了一遍链表
//空间:O(1) 开了三个指针的空间
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL, *cur = head, *next = NULL; //head为头指针指向链表中的第一个元素
while(cur != NULL){
//直到cur指向为空,循环结束。即pre指向链表的最后一个结点,也是新链表的头结点
next = cur->next; cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};

需要考虑以上两个问题,在本题中已经假定1 <= m <= n <= length of list

思路:

/**
* 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 *guard = new ListNode(-1);
guard -> next = head; ListNode *pre = guard;
for(int i=1;i<=m-1;i++)
pre = pre->next; //pre指向需要翻转链表的前一个结点 ListNode *cur = pre->next; //i指向翻转链表的第一个结点
ListNode *post = cur->next;
for(int i=1;i<=n-m;i++){
cur->next = post->next;
post->next = pre->next;
pre->next = post;
post = cur->next;
} return guard -> next;
}
};

完整的翻转链表程序:

// list.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// #include "pch.h"
#include <iostream>
using namespace std; /** * Definition for singly-linked list. */ struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; // 根据n个元素的数组arr创建一个链表, 并返回链表的头 ListNode* createLinkedList(int arr[], int n) { if (n == 0) return NULL; ListNode* head = new ListNode(arr[0]); ListNode* curNode = head; for (int i = 1; i < n; i++) { curNode->next = new ListNode(arr[i]); curNode = curNode->next; } return head; } // 打印以head为头结点的链表信息内容 void printLinkedList(ListNode* head) { ListNode* curNode = head; while (curNode != NULL) { cout << curNode->val << " -> "; curNode = curNode->next; } cout << "NULL" << endl; return; } // 释放以head为头结点的链表空间 void deleteLinkedList(ListNode* head) { ListNode* curNode = head; while (curNode != NULL) { ListNode* delNode = curNode; curNode = curNode->next; delete delNode; } return; } // 206. Reverse Linked List // https://leetcode.com/problems/reverse-linked-list/description/ // 时间复杂度: O(n) // 空间复杂度: O(1) class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* pre = NULL; ListNode* cur = head; while (cur != NULL) { ListNode* next = cur->next; cur->next = pre; pre = cur; cur = next; } return pre; } }; int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr) / sizeof(int); ListNode* head = createLinkedList(arr, n); printLinkedList(head); ListNode* head2 = Solution().reverseList(head); printLinkedList(head2); deleteLinkedList(head2); return 0; }

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur = head;
while(cur && cur->next){
ListNode* post = cur->next;
if(post->val == cur->val){
cur->next = post->next;
delete post;
}
else
cur = cur->next;
} return head;
}
};

思路:在链表中,可以通过创建两个新的头结点指针,来分别指向小于x的结点和大于等于x的结点,遍历结束之后,再将两个新的链表重新连接起来。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *l_head = NULL, *l_tail = NULL;
ListNode *r_head = NULL, *r_tail = NULL;
ListNode *p = head; while(p){
if(p->val < x){
if(l_tail){
l_tail->next = p;
l_tail = l_tail->next;
}
else
l_head = l_tail = p;
}
else{
if(r_tail){
r_tail->next = p;
r_tail = r_tail->next;
}
else
r_head = r_tail = p;
}
p = p->next;
} if(r_tail)
r_tail->next = NULL;
if(l_tail)
l_tail->next = r_head; return l_head?l_head:r_head;
}
};

思路:先将偶数索引的第一个节点记录下来,然后将奇数索引和偶数索引分开为两个链表,最后将奇数索引的最后一个节点指向偶数索引的第一个节点。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if(head==NULL || head->next == NULL)
return head;
ListNode* odd = head;
ListNode* even = head->next;
ListNode* t = even;
while(even!=NULL && even->next!=NULL){
odd->next = even->next;
odd = odd->next;
even->next = odd->next;
even = even->next;
}
odd->next = t;
return head;
}
};

本题中是两个非负整数,且数字除0外前面不会有0。

思路:

1.首先用两个指针,分别同时遍历两个链表,按位相加,设置相应进位标志。
2.当两个链表长度不一致时,结束按位相加的遍历之后,将剩余元素链接接上。(所以需要判断链表当前要计算的结点是否存在)
3.需要注意连续进位。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* res = new ListNode(0);
ListNode* p = res;
int add = 0;
while(l1 || l2 || add){
int n1 = l1 ? l1->val : 0;
int n2 = l2 ? l2->val : 0;
int sum = n1 + n2 + add;
add = sum/10;
p->next = new ListNode(sum%10);
p = p->next;
if(l1) l1 = l1->next;
if(l2) l2 = l2->next;
}
return res->next;
}
};

思路:使用栈的先进后出结构来实现从链表的后面往前面取数字。res记录当前两个结点的和,然后新建一个进位结点head赋值为sum/10,若没有进位就是0。把head的后继连到res,再把res指向head。直到循环退出后,返回res(返回时要判断res为0则返回res->next)。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
stack<int> s1, s2;
while(l1){
s1.push(l1->val);
l1 = l1->next;
}
while(l2){
s2.push(l2->val);
l2 = l2->next;
}
int sum = 0;
ListNode* res = new ListNode(0);
while(!s1.empty() || !s2.empty()){
if(!s1.empty()){
sum += s1.top();
s1.pop();
}
if(!s2.empty()){
sum += s2.top();
s2.pop();
}
res->val = sum % 10;
ListNode* head = new ListNode(sum/10);
head->next = res;
res = head;
sum /= 10;
}
return res->val == 0 ? res->next : res;
}
};

链表 206 Reverse Linked List, 92,86, 328, 2, 445的更多相关文章

  1. (链表) 206. Reverse Linked List

    Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...

  2. 206. Reverse Linked List + 92. Reverse Linked List II

    ▶ 关于单链表翻转的两个问题. ▶ 206. 翻转整个单链表. ● 自己的代码,9 ms,使用了递归. class Solution { public: ListNode* reverseList(L ...

  3. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  4. 206. Reverse Linked List - LeetCode

    Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...

  5. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  6. LeetCode 92. 反转链表 II(Reverse Linked List II)

    92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...

  7. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  9. [刷题] 206 Reverse Linked List

    要求 反转一个链表 不得改变节点的值 示例 head->1->2->3->4->5->NULL NULL<-1<-2<-3<-4<-5 ...

随机推荐

  1. Tensorflow Mask-RCNN训练识别箱子的模型运行结果(练习)

    Tensorflow Mask-RCNN训练识别箱子的模型

  2. Linux下DNS配置

    一.本机DNS配置 参考:http://blog.sina.com.cn/s/blog_68d6e9550100k3b7.html 二.DNS服务器搭建 http://toutiao.com/i631 ...

  3. Luogu 1099 树网的核

    bzoj1999 数据加强版(n <= 5e5) 较早的noip题,值得研究 重要结论:直径的最长性,任何从直径中离开直径的点到它离开的点的距离,都不会比直径的另一端到它离开的点长(否则就有新的 ...

  4. vuex 数据绑定

    操作文档: 安装vuex: cnpm install vuex --save   文档介绍: https://vuex.vuejs.org/guide/modules.html   import Vu ...

  5. 浅谈Android内存管理

    最近在网上看了不少Android内存管理方面的博文,但是文章大多都是就单个方面去介绍内存管理,没有能全局把握,缺乏系统性阐述,而且有些观点有误,仅仅知道这些,还是无法从整体上理解内存管理,对培养系统优 ...

  6. Python基础入门-os模块

    今天我们来介绍一下os模块中常用的一些方法,当然python中的os模块中提供的使用方法有很多,但是这里面小编会列举出来一些和实际工作中应用的相关的方法,而且会有一些实际的例子方便大家对os模块理解. ...

  7. DPF.Android.Native.Components.v2.8.1 for delphi xe6 使用DPFJAlertDialog遇到的问题

    使用DPFJAlertDialog控件时发现DPFJAlertDialog1Click不能捕获到对话框到底按了那个按键,上网搜索后找到了解决方法: 打开DPF.Android.JAlertDialog ...

  8. AES加密 AESCrypt 类

    /// <summary> /// AES加密 /// </summary> public sealed class AESCrypt { /// <summary> ...

  9. 2014-4-2解决无法访问github和google的问题

    github是个好地方,但是上不去就蛋疼了. 今天github上不去,果断f12下,看下network,发现里面好多请求都是指向 github.global.ssl.fastly.net这个域名的,然 ...

  10. 函数LEN()使用方法

    string pro_sql = string.Format("select pr_bianma from tb_products where pr_bianma like '%120201 ...