206. Reverse Linked List(LeetCode)

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(ListNode* head) {
    ListNode * pre=NULL;
        ListNode * current;
        while (head)
        {
            current = head;
            head = head->next;
            current->next=pre ;
            pre = current;
        }
        return pre;

    }
};
时间: 2024-08-24 09:55:21

206. Reverse Linked List(LeetCode)的相关文章

206. Reverse Linked List (LL)

熟悉了一下linkedlist的构造和处理 1 class Solution { 2 public ListNode reverseList(ListNode head) { 3 if(head == null) return head; 4 if(head.next == null) return head; 5 6 ListNode a = head; 7 ListNode b = head.next; 8 ListNode c = a; 9 head.next = null; 10 whi

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 这道题比较简单,主要是给

【leetcode】Reverse Linked List(easy)

Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList = NULL, * tmp = NULL; while(head != NULL) { tmp = rList; rList = head; head = head->next; rList->next = tmp; } return rList; }

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

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) {}  * };

Reverse Linked List II leetcode java

题目: 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

119. Pascal's Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"