剑指offer第15题(反转链表)
来源:互联网 发布:网络购物平台有哪些 编辑:程序博客网 时间:2024/06/06 14:00
题目:输入一个链表,反转链表并输出反转后链表的头结点。
思想:将链表反转即可,值得注意的是如果当前节点指向了前一个节点,我们就需要一个东西存储当前指针之前指向的后一个节点,否则无法在链表中遍历到后一个结点。
java代码:
/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode ReverseList(ListNode head) { if(head==null){ return null; } ListNode pre=null; ListNode next=null; while(head!=null){ next=head.next; head.next=pre; pre=head; head=next; } return pre; }}python代码:
# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: # 返回ListNode def ReverseList(self, pHead): if pHead==None: return None if pHead.next==None: return None while pHead!=None: next=pHead.next pHead.next=pre pre=pHead pHead=next return pre
阅读全文
0 0
- 剑指offer第15题(反转链表)
- 剑指offer第42题:单词反转
- 剑指Offer(15)______反转链表
- 剑指offer 15-反转链表
- 剑指offer(15)—反转链表
- 牛客网-剑指offer-15-反转链表
- 【剑指offer】题16:反转链表
- 反转链表(剑指offer)
- 剑指offer(17):反转链表
- 剑指offer(12)-反转链表
- 剑指offer(十五)反转链表
- 剑指Offer——(15)反转链表
- 【剑指offer-解题系列(15)】反转链表
- 剑指offer:反转链表
- 剑指offer--反转链表
- 剑指offer--反转链表
- 剑指Offer-反转链表
- 《剑指offer》反转链表
- 广州地铁五号线PSCADA系统介绍
- oracle数据库--SQL基础
- [UVA 107] The Cat int the Hat 数学公式和细节啊细节
- imx6ul linux实现MQS播放音乐
- 为什么点击按钮不会触发onclick
- 剑指offer第15题(反转链表)
- 步进电机驱动芯片
- python 循环中显示进度条
- POJ1195(树状数组)
- Building a RESTful Web Service
- burpsuite爆破密码(含验证码)
- 编程练习总结
- 2017年TOP100summit开幕在即, 15位大咖担任联席主席甄选最值得学习的100个研发案例
- 287. Find the Duplicate Number