Lintcode35 Reverse Linked List solution 题解

【题目描述】

Reverse a linked list.

翻转一个链表

【题目链接】

http://www.lintcode.com/en/problem/reverse-linked-list/

【题目解析】

这题要求我们翻转[m, n]区间之间的链表。对于链表翻转来说,几乎都是通用的做法,譬如p1 -> p2 -> p3 -> p4,如果我们要翻转p2和p3,其实就是将p3挂载到p1的后面,所以我们需要知道p2的前驱节点p1。

我们首先遍历得到第m - 1个node,也就是pm的前驱节点。然后依次遍历,处理挂载问题就可以了。

【参考答案】

http://www.jiuzhang.com/solutions/reverse-linked-list/

时间: 2024-10-23 11:55:11

Lintcode35 Reverse Linked List solution 题解的相关文章

Lintcode380 Intersection of Two Linked Lists solution 题解

[题目描述] Write a program to find the node at which the intersection of two singly linked lists begins. Notice: If the two linked lists have no intersection at all, return null. The linked lists must retain their original structure after the function re

【leetcode刷题笔记】Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the following condition:1 ≤ m ≤ n ≤ lengt

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

Leetcode-206 Reverse Linked List

#206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseList(ListNo

LeetCode: Reverse Linked List

LeetCode: Reverse Linked List Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the follo

Leetcode:Reverse Linked List II 反转链表区间

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given   1->2->3->4->5->NULL,  m = 2 and n = 4, return  1->4->3->2->5->NULL. Note:Given m, n satisfy the following

[LintCode] Reverse Linked List 倒置链表

Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1->2->3, the reversed linked list is 3->2->1 Challenge Reverse it in-place and in one-pass LeetCode上的原题,请参见我之前的博客Reverse Linked List. 解法一: class

leetCode 206. Reverse Linked List 反转链表

206. Reverse Linked List Reverse a singly linked list. 反转一个链表. 思路: 采用头插法,将原来链表重新插一次返回即可. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     ListNode *next;  *     ListNode(int x) : val(x), next(NULL) {}  * };

leetcode - Reverse Linked List II

leetcode - Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:Given m, n satisfy the fol