【题目要求直接翻转链表,而非申请新的空间】

这道题的一个关键在于,当m=1时,需要翻转的链表段前没有其他的结点(leetcode的测试用例不含头结点),这个特例给解题带来了一点小小的困难。一个比较直观、比较方便的想法是在链表中插入一个头结点,这样处理起来方便很多。除此之外,还要注意各种循环边界条件的设置。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
struct ListNode * pre=(struct ListNode *)malloc(sizeof(struct ListNode));
pre->next=head;
head=pre; //插入一个头结点
struct ListNode * begin;
int i=;
while(i<m-)
{
pre=pre->next;
i++;
}
begin=pre->next;
for(i=m;i<n;i++)
{
struct ListNode *p=begin->next;
begin->next=p->next;
p->next=pre->next;
pre->next=p;
} return head->next; }

在leetcode上看到一个很简洁的代码,基本思路大致相同灵活地运用了指针,无需添加头结点,膜拜!

ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode **pre = &head; //pre中存储head指针的地址
int steps = m;
while (--steps) { pre = &(*pre)->next;}
ListNode *cur = *pre; //指向翻转链表的第一个结点
for (int i = m; i < n; i++) {
ListNode *next_n = cur->next;
cur->next = next_n->next;
next_n->next = *pre;
*pre = next_n;
}
return head;
}

链表-Reverse Linked List II的更多相关文章

  1. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

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

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

  3. 14. Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  4. 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)

    题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...

  5. lc面试准备:Reverse Linked List II

    1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...

  6. 【LeetCode练习题】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

  7. 反转链表 Reverse Linked List

    2018-09-11 22:58:29 一.Reverse Linked List 问题描述: 问题求解: 解法一:Iteratively,不断执行插入操作. public ListNode reve ...

  8. leetcode -day30 Reverse Linked List II

    1.  Reverse Linked List II  Reverse a linked list from position m to n. Do it in-place and in one- ...

  9. LeetCode解题报告—— Reverse Linked List II & Restore IP Addresses & Unique Binary Search Trees II

    1. Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass ...

随机推荐

  1. C语言字符和字符串随记

    ==========================第11章 字符和字符串函数==========================震惊:字符串常量属于静态存储类,常量引号中的内容作为指向该字符串存储位 ...

  2. Android源码下载

    Android源码下载 1.安装git 2.安装repo 从这里 https://dl-ssl.google.com/dl/googlesource/git-repo/repo 下载repo文件 3. ...

  3. python魔术方法

    在类中有一些特殊的方法具有特殊的意义,比如__init__和__del__方法,它们的重要性我们已经学习过了. 一般说来,特殊的方法都被用来模仿某个行为.例如,如果你想要为你的类使用x[key]这样的 ...

  4. centos6.5 eclipse C/C++开发环境

  5. HDOJ1166 敌兵布阵

    赤裸裸的线段树,借个模板,改写一下即可. 代码: #include<iostream> #include<cstdio> #include<stdio.h> #in ...

  6. wx.Notebook

    wx.Notebook This class represents a notebook control, which manages multiple windows with associated ...

  7. uva 10453 - Make Palindrome(dp)

    题目链接:10453 - Make Palindrome 题目大意:给出一个字符串,通过插入字符使得原字符串变成一个回文串,要求插入的字符个数最小,并且输出最后生成的回文串. 解题思路:和uva 10 ...

  8. POJ 2774 Long Long Message&&HDU 1403 Longest Common Substring&&COJ 1203

    后缀数组的买1送2题... HDU的那题数据实在是太水了,后来才发现在COJ和POJ上都是WA..原因在一点:在建立sa数组的时候里面的n应该是字符串长度+1....不懂可以去看罗大神的论文... 就 ...

  9. BZOJ 1024 SCOI 2009 生日快乐 深搜

    题目大意:有一块蛋糕,长为X,宽为Y.如今有n个人来分这块蛋糕,还要保证每一个人分的蛋糕的面积相等.求一种分法,使得全部的蛋糕的长边与短边的比值的最大值最小. 思路:刚拿到这个题并没有什么思路.可是定 ...

  10. jquery动态连接节点

    <1> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...