问题:反转链表

要求:输入一个链表,反转链表后,输出新链表的表头。

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6             val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* ReverseList(ListNode* pHead) {
12
13     }
14 };

对于翻转链表的解法,在博客链表ADT实现中已经完成,只是将其定义为了链表的一种方法,将其代码稍加修改就可以作为此题的解答,代码如下:

 1 struct ListNode {
 2     int val;
 3     struct ListNode *next;
 4     ListNode(int x) :
 5         val(x), next(NULL) {
 6     }
 7 };
 8
 9 class Solution {
10 public:
11     ListNode* ReverseList(ListNode* pHead) {
12         if (pHead == nullptr || pHead->next == nullptr)
13             return pHead;
14         ListNode *pre = pHead;
15         ListNode *cur = pHead->next;
16         ListNode *temp = pHead->next->next;
17         while (cur){
18             temp = cur->next;
19             cur->next = pre;
20             pre = cur;
21             cur = temp;
22         }
23         // 末尾元素指针置空
24         pHead->next = nullptr;
25         // 头指针
26         ListNode *newHead = pre;
27         return newHead;
28     }
29 };

代码验证:

若原链表为1(100)->2(200)->3(300),括号中的数字为节点的地址。将原链表反转,则反转之后 head = 300;

以下为验证代码,代码先输出链表,紧接着下一行输出链表每个元素的地址。

  1 #include<iostream>
  2 using namespace std;
  3
  4 class Node {
  5 public:
  6     int data;
  7     Node *next;
  8     Node(int da):
  9         data(da), next(NULL){}
 10 };
 11
 12 class List{
 13 public:
 14     Node *head;
 15     List(): head(NULL){}
 16     ~List(){
 17         delete head;
 18         cout<<"The list is deleted."<<endl;
 19     };
 20     int size(); // 链表长度
 21     void printList(); // 打印链表
 22     void insert(int position, int value); // 指定位置插入
 23     void insertHead(int value); // 插入到最前
 24     void insertTail(int value); // 插入到最后
 25     void reverse();
 26 };
 27
 28 // 返回链表大小
 29 int List::size(){
 30     Node *p = head;
 31     int index = 0;
 32     while(p != NULL){
 33         index++;
 34         p = p->next;
 35     }
 36     return index;
 37 }
 38
 39 // 打印链表
 40 void List::printList(){
 41     Node *p = head;
 42     while(p != NULL){
 43         cout<<p->data<<" ";
 44         p = p->next;
 45     }
 46     cout<<endl;
 47
 48     p = head;
 49     while(p != NULL){
 50         cout<<p<<" ";
 51         p = p->next;
 52     }
 53     cout<<endl<<endl;
 54 }
 55
 56 // 在position位置插入value
 57 void List::insert(int position, int value){
 58     if(position<0 || position>List::size()){
 59         cout<<"position error, please check."<<endl;
 60         return ;
 61     }
 62     Node *s = new Node(value); // new node
 63     Node *p = head;
 64     if(head == NULL){ // isEmpty = true
 65         head = s;
 66     }
 67     else{ // isEmpty = false
 68         if(position == 0){
 69             s->next = p;
 70             head = s;
 71         }
 72         else{
 73             int index = 0;
 74             while(index != position-1){
 75                 p = p->next;
 76                 index++;
 77             }
 78             s->next = p->next;
 79             p->next = s;
 80         }
 81     }
 82     if (position == 0)
 83         cout<<"insert "<<value<<" at the first."<<endl;
 84     else if (position == List::size())
 85         cout<<"insert "<<value<<" at the tail."<<endl;
 86     else
 87         cout<<"insert "<<value<<" at position "<<position<<endl;
 88 }
 89
 90 // 头部插入
 91 void List::insertHead(int value){
 92     List::insert(0, value);
 93 }
 94
 95 // 尾部插入
 96 void List::insertTail(int value){
 97     List::insert(List::size(), value);
 98 }
 99
100 // 反转链表
101 void List::reverse(){
102     if (head == NULL || head->next == NULL)
103         return ;
104     Node *prev = head;
105     Node *cur = head->next;
106     Node *temp = head->next->next;
107     while(cur){
108         temp = cur->next;
109         cur->next = prev;
110         prev = cur;
111         cur = temp;
112     }
113     head->next = NULL; // 更新末尾元素的指针
114     head = prev; // 更新头结点
115 }
116
117 int main() {
118     List l1;
119     l1.insertTail(6);
120     l1.insertHead(7);
121     l1.insert(1, 5);
122     l1.insert(0, 16);
123     l1.insert(2, 56);
124     l1.insert(0, 169);
125     l1.insert(6, 16);
126     cout<<endl<<"The list is:"<<endl;
127     l1.printList();
128     l1.reverse();
129     cout<<endl<<"The reversed list is:"<<endl;
130     l1.printList();
131     return 0;
132 }

View Code

运行结果:

 1 insert 6 at the first.
 2 insert 7 at the first.
 3 insert 5 at position 1
 4 insert 16 at the first.
 5 insert 56 at position 2
 6 insert 169 at the first.
 7 insert 16 at position 6
 8
 9 The list is:
10 169 16 7 56 5 6 16
11 0x661a70 0x661a50 0x661a30 0x661a60 0x661a40 0x661a20 0x661a80
12
13
14 The reversed list is:
15 16 6 5 56 7 16 169
16 0x661a80 0x661a20 0x661a40 0x661a60 0x661a30 0x661a50 0x661a70
17
18 The list is deleted.

可见,地址依次倒序,说明代码正确。

转载于:https://www.cnblogs.com/iwangzhengchao/p/9774528.html

剑指offer---反转链表相关推荐

  1. 剑指offer -- 反转链表

    先摆明重要性 剑指offer -- 反转链表  校招原题 解法一:迭代 翻转 即 将所有节点的next指针指向前驱节点. 由于是单链表,我们在迭代时不能直接找到前驱节点,所以我们需要一个额外的指针保存 ...

  2. 【链表】剑指offer:反转链表

    描述 给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头. 要求:空间复杂度 O(1),时间复杂度 O(n) . 当输入链表{1 ...

  3. 剑指offer反转链表(C++实现|测试用例|迭代法和递归法)

    方法1:迭代法 代码: #include<iostream> using namespace std; struct ListNode { int val; ListNode *next; ...

  4. 【每日一题】剑指 Offer 22. 链表中倒数第k个节点

    剑指 Offer 22. 链表中倒数第k个节点

  5. 【LeetCode】剑指 Offer 22. 链表中倒数第k个节点

    [LeetCode]剑指 Offer 22. 链表中倒数第k个节点 文章目录 [LeetCode]剑指 Offer 22. 链表中倒数第k个节点 一.遍历 二.双指针 总结 一.遍历 先遍历统计链表长 ...

  6. 力扣—— 19. 删除链表的倒数第 N 个结点(java)、剑指 Offer 22. 链表中倒数第k个节点(java)

    19. 删除链表的倒数第 N 个结点(java) 一.题目描述 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. 进阶:你能尝试使用一趟扫描实现吗? 输入:head = [1,2,3 ...

  7. 每日一题——剑指 Offer24反转链表

    如果您是第一次看我写的博客,可以给我点个赞并关注我吗,我会持续分享更多有意思的干货. 文章目录 1 题目 2 思路 3 代码 4 小结 1 题目 剑指 Offer 24. 反转链表 定义一个函数,输入 ...

  8. 剑指Offer #14 链表中倒数第k个结点(快慢指针) | 图文详解

    题目来源:牛客网-剑指Offer专题 题目地址:链表中倒数第k个结点 题目描述 输入一个链表,输出该链表中倒数第k个结点. 节点结构如下: public class ListNode {int val ...

  9. 剑指offer之链表续

    面试题17:合并两个有序链表 这道题用递归,很容易实现,但是一定要注意代码鲁棒性 下面是源码: public static ListNode MergeList(ListNode head1,List ...

  10. 《剑指offer》-- 链表中倒数第k个节点、反转链表、合并两个排序的链表

    一.链表中倒数时第k个节点: 1.题目: 输入一个链表,输出该链表中倒数第k个结点. 2.解题思路:单链表具有单向移动的特性. (1)第一种:先遍历链表,算出链表节点数count,第二次直接遍历到第c ...

最新文章

  1. 全球著名 CMS 产品大全
  2. 暂存单点传播帧,以及使用 TIM 来传递
  3. 用Excel的VBA实现文本匹配与替换
  4. dhcp怎么开启 linux,怎么开启DHCP服务器啊
  5. centos7下安全访问远程服务器
  6. Hadoop学习笔记—15.HBase框架学习(基础知识篇)
  7. centos7修改服务器密码忘记,Centos7忘记root密码怎么修改
  8. Nginx限速模块初探
  9. 字符串匹配算法——KMP算法学习
  10. linux----LAMP之编译安装apache
  11. 更换系统后无法catkin_make universal robot 包
  12. Python--网络编程-----基于UDP协议的套接字不会发生粘包
  13. 2019年三峡大学计算机考研名单,三峡大学2019硕士研究生复试录取方案
  14. linux服务器弱密码检查,linux系统弱密码检测
  15. QT视频采集之编码Enc和录像Rec
  16. 计算机网络典型的通信协议有,常用的通信网络协议有哪几种
  17. Vue实例--音乐播放器:歌单数据接口分析
  18. python画图时设置分辨率和画布大小-plt.figure()
  19. 地址总线、字长和寻址空间的关系
  20. XML Publisher 模板设计技巧

热门文章

  1. 性能更好的js动画实现方式——requestAnimationFrame
  2. jquery遍历函数siblings()
  3. Mac系统修改root用户密码,mac切换root用户登录实例演示
  4. Python 技术篇-用flask服务实现mac本地文件上传至windows服务器指定文件夹下实例演示
  5. Dbvis数据库连接工具将查询出数据转化为sql插入语句方法
  6. CTFshow 文件上传 web156
  7. CTFshow 命令执行 web75
  8. 指针数组(主要用于二维的数组)
  9. opencv运行时exe 无法写进去的解决方法
  10. boxfilter 函数