当前位置:网站首页>学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
边栏推荐
- QR code generation API interface, which can be directly connected as an A tag
- (2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
- 一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
- (5) Modules and packages, encoding formats, file operations, directory operations
- Alfa: 1 vulnhub walkthrough
- Using PHPMailer send mail
- Phpstudy安装Thinkphp6(问题+解决)
- Xiaoyao multi-open emulator ADB driver connection
- xxe of CTF
- hackmyvm: may walkthrough
猜你喜欢
C language uses stack to calculate infix expressions
Shuriken: 1 vulnhub walkthrough
CSRF(跨站请求伪造)
TypeScript error error TS2469, error TS2731 solution
[sebastian/diff] A historical change extension library for comparing two texts
Thread Pool (Introduction and Use of Thread Pool)
kali安装IDEA
How to log in to Alibaba Cloud server using the admin account
Phonebook
Shuriken: 1 vulnhub walkthrough
随机推荐
SQL注入(6)
(7) 浅学 “爬虫” 过程 (概念+练习)
web安全之目录遍历
vim edit mode
12. What is JS
PHP Foundation March Press Announcement Released
[league/climate] A robust command-line function manipulation library
ES6 three-dot operator, array method, string extension method
Eric靶机渗透测试通关全教程
hackmyvm: again walkthrough
Several interesting ways to open PHP: from basic to perverted
hackmyvm: kitty walkthrough
Masashi: 1 vulnhub walkthrough
PHP image compression to specified size
Alibaba Cloud MySQL 5.7 installation and some major problems (total)
PHP realizes the automatic reverse search prompt of the search box
CTF入门笔记之ping
[phpunit/php-timer] A timer for code execution time
A network security guinea pig's learning path - scripting of advanced usage of nmap
The learning path of a network security mouse - the basic use of nmap