leetcode_206题——Reverse Linked List(链表)

Reverse Linked List

Total Accepted: 1726 Total Submissions: 4378My Submissions

Question Solution

Reverse a singly linked list.

click to show more hints.

Hide Tags

Linked List

Have you met this question in a real interview?

Yes

No

Discuss

这道题比较简单,主要是给链表反序,所以可以设置三个指针,ptr0,ptr1指向要交换的两个结点,ptr2在交换之前先指向ptr1的下一个结点,

这样好为下一次交换做准备,即再往后移就可以了,直到末尾

#include<iostream>
using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
	 };

ListNode* reverseList(ListNode* head) {
	if(head==NULL||head->next==NULL)
		return head;
	ListNode* ptr0=head;
	ListNode* ptr1=head->next;
	ListNode* ptr2;
	ptr0->next=NULL;
	while(1)
	{
		ptr2=ptr1->next;
		ptr1->next=ptr0;
		if(ptr2==NULL)
			break;
		ptr0=ptr1;
		ptr1=ptr2;
	}
	return ptr1;
}
int main()
{

}

  

时间: 2024-10-09 09:31:52

leetcode_206题——Reverse Linked List(链表)的相关文章

leetcode_92题——Reverse Linked List II(链表操作)

Reverse Linked List II Total Accepted: 40420 Total Submissions: 154762My Submissions Question Solution 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, retur

Leetcode 206 Reverse Linked List 链表

将单向链表反转 完成如图操作,依次进行即可 1 2 3 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* h

leetcode_234题——Palindrome Linked List(链表)

Palindrome Linked List Total Accepted: 5466 Total Submissions: 23472My Submissions Question Solution 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? Hide Tags Linked List Two Pointer

【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: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(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode 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 sati