当前位置:网站首页>学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
边栏推荐
猜你喜欢
![[sebastian/diff] A historical change extension library for comparing two texts](/img/c7/ea79db7a5003523ece7cf4f39e4987.png)
[sebastian/diff] A historical change extension library for comparing two texts

Praying: 1 vulnhub walkthrough

hackmyvm: juggling walkthrough
![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

(4) 函数、Bug、类与对象、封装、继承、多态、拷贝

Introduction to PHP (self-study notes)

13. JS output content and syntax

Phpstudy安装Thinkphp6(问题+解决)

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

After Alibaba Cloud sets up domain name resolution redirection, I cannot use Chrome to access it
随机推荐
13. JS output content and syntax
PHP deserialization vulnerability
PHP入门(自学笔记)
DNS详解
hackmyvm-hopper预排
After Alibaba Cloud sets up domain name resolution redirection, I cannot use Chrome to access it
file contains vulnerabilities
Cookie is used to collect the admin privileges CTF foundation problem
MySql Advanced -- Constraints
The learning path of a network security mouse - the basic use of nmap
CTF入门笔记之SQL注入
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
c语言用栈实现计算中缀表达式
1. Beginning with PHP
hackmyvm-hopper walkthrough
[sebastian/diff] A historical change extension library for comparing two texts
WeChat applet development video loading: [Rendering layer network layer error] Failed to load media
CTF入门之php文件包含
OPENSSL基本实验以及OPENSSL详解
利用cookie获取admin权限 CTF基础题