Reverse a singly 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) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL){
return head;
}
ListNode *p1,*p2,*p3;
p1 = head;
p2 = p1->next;
if (p2 == NULL){
return head;
}
p3 = p2->next;
while (p2 != NULL){
p2->next = p1;
p1 = p2;
p2 = p3;
if (p3 != NULL){
p3 = p3->next;
}
}
head->next = NULL;
return p1; }
};
Reverse a singly linked list的更多相关文章
- LeetCode 206 Reverse a singly linked list.
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [轉]Reverse a singly linked list
Reverse a singly linked list http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...
- [cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...
- 单链表反转(Singly Linked Lists in Java)
单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 package dsa.linkedlist; public class Node<E> ...
- [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...
- Singly Linked List
Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...
- Reverse a Singly LinkedList
Reverse a Singly LinkedList * Definition for singly-linked list. * public class ListNode { * int val ...
- [TS] Implement a singly linked list in TypeScript
In a singly linked list each node in the list stores the contents of the node and a reference (or po ...
- amazon o2 - reverse second half linked list
public ListNode reverseBetween(ListNode head, int m, int n) { if(head==null) return head; ListNode s ...
随机推荐
- ssh secure shell
ssh secure shell 和securecrt xhell一样,都是终端工具
- php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组
php如何遍历多维的stdClass Object 对象,php的转换成数组的函数只能转换外面一丛数组 (2012-09-10 19:58:49) 标签: 杂谈 分类: 网页基础知识 php如何遍历多 ...
- 移动端性能优化动态加载JS、CSS
JS CODE (function() { /** * update: * 1.0 */ var version = "insure 1.1.0"; var Zepto = Zep ...
- MYSQL中创建存储过程实现向表中循环插入数据
首先在test数据库中先创建一个表test: CREATE TABLE test( ID INT PRIMARY KEY AUTO_INCREMENT ,test_name VARCHAR(20),t ...
- ios 父VIew的宽度 等于较大view的宽度,并且垂直居中
白色view上面有两个子View,红色view和橙色view.白色view的宽度等于橙色view和红色view宽度较大的一个,并且橙色view和红色view垂直居中, Masonry布局如下: ...
- Spring AOP 切面编程的方法
spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...
- Java反射的三种实现方式
Foo foo = new Foo(); 第一种:通过Object类的getClass方法 Class cla = foo.getClass(); 第二种:通过对象实例方法获取对象 Class cla ...
- 使用CMD命令设置IP
使用CMD命令设置IP,将下面文本保存为bat文件后执行 netsh interface ip set address name="本地连接" source=static addr ...
- magento -- 解决magento错误:ERROR: Base table or view already exists: 1050 Table ... already exists
相信有更新magento或者,备份转移magento站点的时候可能会碰到类似这样的错误提示: Base table or view already exists: 1050 Table ... alr ...
- RDIFramework.NET Web版介绍
RDIFramework.NET Web版介绍 B/S结构(Browser/Server,浏览器/服务器模式),是WEB兴起后的一种网络结构模式,WEB浏览器是客户端最主要的应用软件.这种模式统一了 ...