当前位置:网站首页>学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
边栏推荐
- PHP Foundation March Press Announcement Released
- DarkHole: 2 vulnhub walkthrough
- 12. What is JS
- (6) Design of student information management system
- vim编辑模式
- Turn trendsoft/capital amount of Chinese capital library
- [sebastian/diff] A historical change extension library for comparing two texts
- GreenOptic: 1 vulnhub walkthrough
- Summary of php function vulnerabilities
- 攻防世界—MISC 新手区1-12
猜你喜欢

SQL: DDL, DML, DQL, DCL corresponding introduction and demonstration

The CTF introductory notes of SQL injection

PHP Foundation March Press Announcement Released

Eric target penetration test complete tutorial

redis未授权访问(4-unacc)

CSRF (Cross Site Request Forgery)

IP access control: teach you how to implement an IP firewall with PHP

CTF entry md5

Kali环境下Frida编写脚本智能提示

hackmyvm-hopper预排
随机推荐
OPENSSL基本实验以及OPENSSL详解
PHP deserialization vulnerability
How to determine the direction based on two coordinate points on the map
Masashi: 1 vulnhub walkthrough
The roll call system and array elements find maximum and minimum values for sorting of objects
Masashi: 1 vulnhub walkthrough
hackmyvm: may walkthrough
(3) string
vim编辑模式
What will be new in PHP8.2?
Turn trendsoft/capital amount of Chinese capital library
PHP入门(自学笔记)
(2) 顺序结构、对象的布尔值、选择结构、循环结构、列表、字典、元组、集合
QR code generation API interface, which can be directly connected as an A tag
MySql Advanced -- Constraints
阿里云MySQL5.7安装以及部分主要问题(总和)
CTF入门之php文件包含
hackmyvm-hopper预排
Summary of php function vulnerabilities
命令执行漏洞