表不支持随机查找,通常是使用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. oracle 创建表 外键约束

    create table usertable( id int primary key, username ) not null, birthday date, sex ), address ) ); ...

  2. 如何从Win7上卸载Sql 2008 R2 Express,再重装

    这两天,因工作需要,需要在一台新的机器上,Win7 64位英文操作系统上,安装Sql Server 2008 R2 Express,安装的过程中出现些问题,在查找问题的过程中,考虑重装 Sql Ser ...

  3. Shell +Cygwinterminal+WinMySQL 传参数授权

    前言:新公司因为部分业务原因有好几百组win机器装MySQL授权登录比较麻烦,简单的写了一个shell传值自动授权的脚本,保存复用. #!/bin/bash #author liding@zlhy.c ...

  4. loj2512 [BJOI2018]链上二次求和

    传送门 分析 咕咕咕 代码 #include<iostream> #include<cstdio> #include<cstring> #include<st ...

  5. HTTP文件上传插件开发文档-JSP

    版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/http-u ...

  6. 编写高质量代码改善C#程序的157个建议——建议20:使用泛型集合代替非泛型集合

    建议20:使用泛型集合代替非泛型集合 在建议1中我们知道,如果要让代码高效运行,应该尽量避免装箱和拆箱,以及尽量减少转型.很遗憾,在微软提供给我们的第一代集合类型中没有做到这一点,下面我们看Array ...

  7. 转Delphi中XLSReadWrite控件的使用(3) 读和写Excel

    unit OpExcell; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Fo ...

  8. 函数LEN()使用方法

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

  9. 《Thinking in Java》 10~

    chapter 10 内部类(P191) 创建内部类 链接到外部类 内部类能访问器外围对象的所有成员,而不需要任何特殊条件. 使用.this与.new package cn.test; public ...

  10. MongoDB 副本集配置,开启账号认证

    MongoDB 自带功能强大的主从,配置也很简单,从零开始花了30分钟搞定 3台以上机器IP: 192.168.1.24, 192.168.1.25, 192.168.1.26, 192.168.1. ...