当前位置:网站首页>NowCoder 反转链表
NowCoder 反转链表
2022-07-04 12:51:00 【范谦之】
描述
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围: 0 ≤ n ≤ 10000 0\leq n\leq10000 0≤n≤10000
要求:空间复杂度 O(1),时间复杂度 O(n)。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:
思路
使用递归方法反转链表。
代码
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode reverse = ReverseList(head.next);
head.next.next = head;
head.next = null;
return reverse;
}
边栏推荐
- R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
- Test evaluation of software testing
- GCC【6】- 编译的4个阶段
- 【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
- 10.(地图数据篇)离线地形数据处理(供Cesium使用)
- mac redis安装与使用,连接远程服务器 redis
- The game goes to sea and operates globally
- R language uses follow up of epidisplay package The plot function visualizes the longitudinal follow-up map of multiple ID (case) monitoring indicators, and uses stress The col parameter specifies the
- Test process arrangement (3)
- Install MySQL
猜你喜欢
392. Judgement subsequence
Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
Unittest框架中引入TestFixture
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
1200. Minimum absolute difference
sharding key type not supported
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire
JVM memory layout detailed, illustrated, well written!
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
Incremental ternary subsequence [greedy training]
随机推荐
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
Xcode 异常图片导致ipa包增大问题
What is the real meaning and purpose of doing things, and what do you really want
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Unittest中的TestSuite和TestRunner
Product identification of intelligent retail cabinet based on paddlex
Innovation and development of independent industrial software
Huahao Zhongtian rushes to the scientific and Technological Innovation Board: the annual loss is 280million, and it is proposed to raise 1.5 billion. Beida pharmaceutical is a shareholder
Common content type correspondence table
Error in find command: paths must precede expression (turn)
DDD application and practice of domestic hotel transactions -- Code
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
Blob, text geometry or JSON column'xxx'can't have a default value query question
Deming Lee listed on Shenzhen Stock Exchange: the market value is 3.1 billion, which is the husband and wife of Li Hu and Tian Hua
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
Read excel table data
LifeCycle
R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
2022 practice questions and mock exams for the main principals of hazardous chemical business units