【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转
Reverse a singly linked list.
Discuss中的递归解法:
public ListNode reverseList(ListNode head) {
return reverseListInt(head, null);
}
public ListNode reverseListInt(ListNode head, ListNode newHead) {
if(head == null)
return newHead;
ListNode next = head.next;
head.next = newHead;
return reverseListInt(next, head);
}
我的代码:用的是迭代(即循环)的方法
C语言:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
if (head == NULL || head->next == NULL)
return head;
struct ListNode* p = head->next;
struct ListNode* q = p;
head->next = NULL;
while(p) {
p = p->next;
q->next = head;
head = q;
q = p;
} return head;
}
【12_206】Reverse Linked List的更多相关文章
- 【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. F ...
- 【leetcode】Reverse Linked List II (middle)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】Reverse Linked List(easy)
Reverse a singly linked list. 思路:没啥好说的.秒... ListNode* reverseList(ListNode* head) { ListNode * rList ...
- 【链表】 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- ...
- 【Leetcode】【Easy】Reverse Linked List
题目: Reverse a singly linked list. 解题: 反转单链表,不再多介绍了. 如果会“先条件->定参数->确定不变式->验证后条件”的思维方法,一定会bug ...
- 【LeetCode92】Reverse Linked List II★★
题目描述: 解题思路: 题目大意:给定一个链表,反转第m到第n个结点部分,m.n满足1 ≤ m ≤ n ≤ length of list. 解题思路参照LeetCode206题,用迭代法,不过要注意以 ...
- 【LeetCode206】Reverse Linked List★
题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
- 【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. F ...
随机推荐
- Codeforces Round #226 (Div. 2) B
B. Bear and Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 自动化脚本过程中出现This element neither has attached source nor attached Javadoc...的解决方法
This element neither has attached source nor attached Javadoc and hence no Javadoc could be found Ec ...
- UBUNTU 13.04 install Grive
sudo apt-get install software-properties-common sudo apt-get install python-software-properties sudo ...
- winform客户端向web地址传参,怎样去接收参数。
在web端定义js方法去接收客户端传递过来的参数,具体就是获取地址中?后的数据,各个参数用&分割,存储于数组中,获取. 具体如下: //定义获取地址中参数的方法 function GetReq ...
- JQuery 公网 CDN
JQuery 公网 CDN <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js&quo ...
- 将CSDN和WordPress上的旧文章迁移过来
我最早用的博客是CSDN,但是不喜欢CSDN的博客,发文章还要审核,界面做的也很模糊,发个文章还必须选个分类.总之不像是用心在做博客这个功能. 后来,我自己搭建了一个网站(www.wangyufeng ...
- struts标签,<s:textfield>嵌套<s:property>的问题
错误:org.apache.jasper.JasperException: /front/orderList.jsp(110,122) equal symbol expected <s:te ...
- [2015hdu多校联赛补题]hdu5299 Circles Game
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5299 题意: 在欧几里得平面上有n个圆,圆之间不会相交也不会相切,现在Alice和Bob玩游戏,两人轮 ...
- js根据生日计算出年龄
/*根据出生日期算出年龄*/ function jsGetAge(strBirthday){ var returnAge; var strBirthdayArr=strBirthday.split(& ...
- python中的system函数与编码
在调用os.system执行命令时,发现system不能接受unicode的命令.那么命令中却又包含以unicode表示的中文等字符怎么办? ——方法就是将unicode转化为utf8 path = ...