当前位置:网站首页>学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
边栏推荐
- vim编辑模式
- MySql Advanced -- Constraints
- Introduction to PHP (self-study notes)
- (2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
- After Alibaba Cloud sets up domain name resolution redirection, I cannot use Chrome to access it
- file contains vulnerabilities
- 4. PHP array and array sorting
- CSRF (Cross Site Request Forgery)
- hackmyvm: kitty walkthrough
- After the mailbox of the Pagoda Post Office is successfully set up, it can be sent but not received.
猜你喜欢

Praying: 1 vulnhub walkthrough

The CTF introduction of PHP file contains

Phpstudy installs Thinkphp6 (problem + solution)

How to log in to Alibaba Cloud server using the admin account

CSRF(跨站请求伪造)

What are the PHP framework?

hackmyvm: controller walkthrough
![[league/climate] A robust command-line function manipulation library](/img/ce/39114b1c74af649223db97e5b0e29c.png)
[league/climate] A robust command-line function manipulation library
![WeChat applet development video loading: [Rendering layer network layer error] Failed to load media](/img/24/e12a1312aee28a43428b2ae0bfbe00.png)
WeChat applet development video loading: [Rendering layer network layer error] Failed to load media

uniapp | Compilation error after updating with npm update
随机推荐
vim edit mode
一个网络安全小白鼠的学习之路—nmap高级用法之脚本使用
Various ways of AES encryption
攻防世界—MISC 新手区1-12
vim编辑模式
PHP8.2 version release administrator and release plan
CTF-Neting Cup Past Topics
(3) Thinkphp6 database
Phpstudy安装Thinkphp6(问题+解决)
Using PHPMailer send mail
hackmyvm: again walkthrough
(3) string
4. PHP array and array sorting
Basic use of v-on, parameter passing, modifiers
Alfa: 1 vulnhub walkthrough
CTF入门笔记之ping
The learning path of a network security mouse - the basic use of nmap
v-bind usage: class dynamic binding object array style style and function method
Alibaba Cloud MySQL 5.7 installation and some major problems (total)
Offensive and defensive world - novice MISC area 1-12