当前位置:网站首页>学IT,找工作——反转链表
学IT,找工作——反转链表
2022-08-02 03:25:00 【PenguinLeee】
206. 反转链表
给定单链表的头节点 head ,反转链表,并返回反转后的链表。
解:
在脑海里模拟一个形象的解法:
我们现在有两个糖葫芦串,分别是旧的串子和新的串子,打算把山楂球从旧串子串到新串子上。
做法就是:
- 从旧串子上把最顶上的那颗山楂球摘下来,然后插到新的串子上
- 摘下来的山楂球就将是新串的头部。
- 顺便维护一下新串中山楂球的前后关系。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def reverseList(self, head):
""" :type head: ListNode :rtype: ListNode """
# 新串子
head2 = None
# 从旧串子上把山楂球挨个撸下来
traverse = head
# 辅助撸旧串子的变量
pointer = None
while traverse != None:
# 从旧串上把最顶上的那颗山楂球摘下来,然后插到新的串上
pointer = traverse.next
# 从旧串上新摘下来的山楂球就将是新串的头部。
traverse.next = head2
head2 = traverse
# 顺便维护一下新串中山楂球的前后关系。
traverse = pointer
return head2
边栏推荐
- 1. Beginning with PHP
- 机器学习1
- SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration
- hackmyvm-bunny预排
- Kali install IDEA
- Smart Tips for Frida Scripting in Kali Environment
- hackmyvm-random walkthrough
- CTF introductory notes ping
- How to determine the direction based on two coordinate points on the map
- 文件上传漏洞
猜你喜欢
(3) string
(3) 字符串
Kali install IDEA
hackmyvm: controller walkthrough
CTF入门笔记之ping
CTF-Neting Cup Past Topics
一个网络安全小白鼠的学习之路—nmap高级用法之脚本使用
(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
redis未授权访问(4-unacc)
VIKINGS: 1 vulnhub walkthrough
随机推荐
hackmyvm: may walkthrough
[symfony/finder] The best file manipulation library
(6) Design of student information management system
(8) requests, os, sys, re, _thread
hackmyvm: controller walkthrough
xxe of CTF
Thread Pool (Introduction and Use of Thread Pool)
php函数漏洞总结
CTF入门笔记之ping
攻防世界—MISC 新手区1-12
Smart Tips for Frida Scripting in Kali Environment
Kali环境下Frida编写脚本智能提示
WeChat applet development video loading: [Rendering layer network layer error] Failed to load media
GreenOptic: 1 vulnhub walkthrough
(2)Thinkphp6模板引擎**标签
文件包含漏洞
hackmyvm: juggling walkthrough
(7) superficial "crawlers" process (concept + practice)
A network security guinea pig's learning path - scripting of advanced usage of nmap
Scrapy爬虫遇见重定向301/302问题解决方法