题目:

翻转一个链表

样例

给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null

挑战

在原地一次翻转完成

解题:

递归还是可以解的

java程序:

/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
// write your code here
if( head ==null || head.next ==null)
return head;
ListNode second = head.next;
head.next = null;
ListNode res = reverse(second);
second.next = head;
return res;
}
}

总耗时: 2079 ms

Python程序:

"""
Definition of ListNode class ListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of the linked list.
@return: You should return the head of the reversed linked list.
Reverse it in-place.
"""
def reverse(self, head):
# write your code here
if head == None or head.next == None:
return head
second = head.next;
head.next = None
res = self.reverse(second)
second.next = head
return res

总耗时: 265 ms

非递归参考于剑指OfferP113

定义三个节点:

revHead翻转后的头节点

p向前走的节点

prev要插入节点的前一个节点,同时在循环中还有一个节点pNext临时保存p的下一个节点

初始值:p=head,prev = null,revHead = null

在循环中:

先pNext = p.next 临时保存p的下一个节点,防止链表锻炼

p.next = prev p的下一个节点直线prev节点,就是翻转,链接到其前面的一个节点,为了保持每次都能这样链接

prev = p  prev节点向后移动一个节点

最后p = pNext 循环下去

同时要找到链表的头节点

当pNext==null的时候 revHead = p p就是头节点,其实运行结束时候的prev节点就是指向头节点的,单独搞个头节点,看着舒服点

/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: The new head of reversed linked list.
*/
public ListNode reverse(ListNode head) {
// write your code here
ListNode revHead = null;
ListNode p = head;
ListNode prev = null;
while(p!=null){
ListNode pNext = p.next;
if(pNext == null){
// 翻转后的头节点,后面是空,结果
revHead = p;
}
// p的下一个节点指向之前要链接的节点
p.next = prev;
// 要链接的节点 直线p节点,以供下次链接
prev = p;
// p节点更新,指向pNext
p = pNext;
}
return revHead;
}
}

Java Code

总耗时: 1325 ms

"""
Definition of ListNode class ListNode(object): def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param head: The first node of the linked list.
@return: You should return the head of the reversed linked list.
Reverse it in-place.
"""
def reverse(self, head):
# write your code here
p = head
prev = None
revHead = None
while p!=None:
pNext = p.next
if pNext ==None:
revHead = p
p.next = prev
prev = p
p = pNext
return revHead

Python Code

总耗时: 223 ms

lintcode: 翻转链表的更多相关文章

  1. Lintcode 翻转链表

    翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 分析: /** * Definition of ListN ...

  2. [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  3. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  4. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  5. C语言递归,非递归实现翻转链表

    翻转链表作为,链表的常用操作,也是面试常遇到的. 分析非递归分析: 非递归用的小技巧比较多,很容易出错. 递归分析比较简单,在代码里面 代码: #include<stdio.h> #inc ...

  6. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

  7. 【LeetCode题解】25_k个一组翻转链表(Reverse-Nodes-in-k-Group)

    目录 描述 解法一:迭代 思路 Java 实现 Python 实现 复杂度分析 解法二:递归(不满足空间复杂度) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记 ...

  8. LeetCode(15): 每k个一组翻转链表

    hard! 题目描述: 给出一个链表,每 k 个节点为一组进行翻转,并返回翻转后的链表. k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. ...

  9. Leetcode题库——25.k个一组翻转链表

    @author: ZZQ @software: PyCharm @file: ReverseList.py @time: 2018/11/6 15:13 题目要求:给出一个链表,每 k 个节点一组进行 ...

随机推荐

  1. 【Qt】Qt之自定义界面(窗体缩放)【转】

    简述 通过前两节内容,我们实现了自定义窗体的移动,以及自定义标题栏-用来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭. 在这之后,我们还缺少窗体的缩放-当鼠标移动到窗体的边框-左.上.右.下 ...

  2. 一款非常炫酷的jQuery动态随机背景滚动特效

    一款非常炫酷的jQuery动态随机背景滚动特效 图片背景会不停息的滚动,带有那种漂浮的视觉效果,小圈圈飘动. 更好的是还兼容IE6浏览器,大伙可以好好研究研究. 适用浏览器:IE6.IE7.IE8.3 ...

  3. opencv初体验

    http://guoming.me/opencv-config  这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...

  4. mouse_driver

    1:function.h #ifndef FUNCTION_H#define FUNCTION_H #define DRIVER_FUNCTION_ADD_DEVICE#define DRIVER_F ...

  5. iOS中使用自定义字体

    1.确定你的项目工程的Resources下有你要用的字体文件(.ttf或者.odf). 2.然后在你的工程的Info.plist文件中新建一行,添加key为:UIAppFonts,类型为Array或D ...

  6. 简单的C#线程开发实例(隔一秒改变一下Label的Text)

    要实现的效果:点击按纽,窗口上的label上出现1~100数字的变化. 第一个实例(把窗口上的label上文字改成0): using System; using System.Windows.Form ...

  7. 一个Java对象到底占用多大内存

    在网上搜到了一篇博客讲的非常好,里面提供的这个类也非常实用: import java.lang.instrument.Instrumentation; import java.lang.reflect ...

  8. 微软职位内部推荐-Principal Software Developer

    微软近期Open的职位: Contact Person: Winnie Wei (wiwe@microsoft.com ) Work Location: Suzhou/Beijing News is ...

  9. 微软职位内部推荐-Enterprise Architect - BDE - BJ

    微软近期Open的职位: Enterprise ArchitectCloud, HTML5, Big Data and Mobile are technology trends driving pro ...

  10. div+css布局细节问题

    cursor: pointer;在chrome里支持,hand不支持