Lettcode_206_Reverse Linked List

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45739753

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

思路:

(1)题意为给定链表,将其(逆置)翻转。该题在校招笔试面试中出现频率较大。

(2)该题主要考察对链表中指针的考察,其实并不难。首先,考虑设定两个指针fis和sed,分别指向链表的第一个元素和第二个元素,其中fis指向结果链表,需在此将其next置为null;其次,循环对指向第二个元素的sed为空进行判定,如果不为空,则设定指针thd指向第三个元素,将sed的next指向fis,这样就完成了前两个元素的逆置;最后,需将fis和sed的同步后移动,而此时的thd就起到了保存位置信息的作用,移动后得到fis指向sed所在位置,sed指向thd所在位置,这样循环遍历,最终得到的fis即为结果链表。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现如下:

/**
	 *
	 * @param liqq
	 * @return
	 */
	public ListNode reverseList(ListNode head) {
		if (head == null)
			return null;
		if (head != null && head.next == null)
			return head;
		ListNode fis = head;
		ListNode sed = head.next;
		fis.next = null;
		while (sed != null) {
			ListNode thd = sed.next;
			sed.next = fis;
			fis = sed;
			sed = thd;
		}
		return fis;
	}
时间: 2024-12-14 06:20:41

Lettcode_206_Reverse Linked List的相关文章

Palindrome Linked List Leetcode

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 这个follow up要求O(n)的时间和O(1)的空间,可以先reverse一半,然后再对比.只是reverse的时候要考虑奇数个还是偶数个.如果是奇数个的话,就跳过最中间的,从下一个开始reverse. /** * Definition for singly-li

237. Delete Node in a Linked List

1. 问题描述 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 ->

Reverse Linked List

题目: Reverse a singly linked list. cpp: class Solution { public: ListNode* reverseList(ListNode* head) { if(!head || !head->next) return head; ListNode *p1 = head; ListNode *p2 = head->next; ListNode *p3 = head->next->next; p1->next = nullpt

Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have no i

[leedcode 142] Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!).  我们已经得到了结论a=c,那么让两个指针分别从X

Palindrome Linked List

Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 思路: 把链表一分为二,把右边的一半翻转,再逐个比对左右的链表即可. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNo

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 class Solution { public: TreeNode *node = NULL; void flatten(TreeNode *root) { if(root == NULL) return; i

[C++]LeetCode: 60 Intersection of Two Linked Lists

题目: Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have

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