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        while(b != null) {
11            c = a;
12            a = b;
13            b = b.next;
14            a.next = c;
15
16        }
17        return a;
18    }
19 }

原文地址:https://www.cnblogs.com/goPanama/p/9495876.html

时间: 2024-08-29 14:58:13

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

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=

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 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】:Evaluate Reverse Polish Notation (python)

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

终结Linked List(三)

第一篇终结Linked List(一).终结Linked List(二)主要讲了单链表的基础知识,接下来的第二篇主要讲一些比较经典的问题. 一.Count() 给一个单链表和一个整数,返回这个整数在链表中出现了多少次. /* Given a list and an int, return the number of times that int ocucurs in the list. */ int Count(struct node* head,int searchFor) { int cnt

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] 206. Reverse Linked List ☆(反转链表)

Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表.你能否用两种方法解决这道题? 解析 设置三个节点pre.cur.next (1)每次查看cur节点是否为NULL,如果是,则结束循环,获得结果 (2)如果cur节点不是为NULL,则先设置临时变量next为cur的下一个节点 (3)让cur

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", "*"] -&g