【LeetCode】206 - Reverse Linked List

Reverse a singly linked list.

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

Solution 1:iteration

 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* head) {        //runtime:8ms
12         ListNode *p1,*p2,*p3;
13         if(head==NULL||head->next==NULL)return head;
14         p1=head,p2=p1->next;
15         while(p2){
16             p3=p2->next;
17             p2->next=p1;
18             p1=p2;
19             p2=p3;
20         }
21         head->next=NULL;
22         head=p1;
23         return head;
24     }
25 };

Solution 2:recursion

 1 class Solution {
 2 public:
 3     ListNode* reverseList(ListNode* head) {     //runtime:8ms
 4         if(head==NULL||head->next==NULL)return head;
 5
 6         ListNode* p = head->next;
 7         ListNode* n = reverseList(p);
 8
 9         head->next = NULL;
10         p->next = head;
11         return n;
12     }
13 };
时间: 2024-11-08 23:52:55

【LeetCode】206 - Reverse Linked List的相关文章

【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three pointers to achieve this goal. 1) Pointer to last value 2) Pointer to cur p value 3) Pointer to next value Here, showing my code wishes can help u. #incl

【leetcode】92. 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】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

【LeetCode】Evaluate Reverse Polish Notation

题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"]

【Leetcode】Evaluate Reverse Polish Notation JAVA

   一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*&

【LeetCode】345. Reverse Vowels of a String 解题小结

题目: Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Given s = "hello", return "holle". Example 2:Given s = "leetcode", return "leotcede". 应该算不上有难度. class Solution { p

【leetcode】25. Reverse Nodes in k-Group

题目描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. You may not alter the values in the nodes, on

【LeetCode】234 - 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? Hide Tags: Linked List Two Pointers 1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 typedef struct ListNode

【LeetCode】007 Reverse Interger

题目:LeetCode 007 Reverse Interger 题意:将一个整数的数字反转.保留正负符号. 思路:先将整数变成字符串,然后判断是否为负数,或是否含有’+’,然后从字符串末尾开始累计得到新整数即可. 但是还会有特殊情况,即正向为Int范围内,但反转之后会溢出,因此要进行特判. 代码如下: 1 class Solution { 2 public: 3 int reverse(int x) { 4 int len, flag = 1, i = 0; 5 long long ans =