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 ...
随机推荐
- A trip through the Graphics Pipeline 2011_08_Pixel processing – “fork phase”
In this part, I’ll be dealing with the first half of pixel processing: dispatch and actual pixel sha ...
- Range
欢迎转载,转载请注明出处,徽沪一郎. 概要 Scala中Range可以看成是List的特例,Range的包含的元素类型是Int, 本文介绍如何创建Range Range创建 方法一: val r1 = ...
- ios-获取商店已上线app信息
NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@ ...
- 一些asp.net使用
1.英文格式的日期转为中文格式 eg:string englishDate="03-04-2012"; string chineseDate=Regex.replace(engli ...
- StringBuffer与StringBuilder有什么区别
package String比较; /* * StringBuffer与StringBuilder有什么区别 * StringBuilder是JDK5增加的一个新类,功能几乎与StringBuffer ...
- LeetCode Generalized Abbreviation
原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...
- Rsyslog配置文件详解
Rsyslog配置文件详解https://my.oschina.net/0757/blog/198329 # Save boot messages also to boot.log 启动的相关信息lo ...
- thttpd和cgilua安装与运行流程分析
安装 参考如下博文安装thttpd软件 http://blog.csdn.net/21aspnet/article/details/7045845 http://blog.csdn.net/drago ...
- AngularJS Best Practices: ASP.NET MVC Directory Structure
/Content----- images/ // Images for your app----- css/ // Styles for your app/Scripts----- libs/ // ...
- TCP中异常关闭链接的意义 异常关闭的情况
终止一个连接的正常方式是发送FIN. 在发送缓冲区中 所有排队数据都已发送之后才发送FIN,正常情况下没有任何数据丢失. 但我们有时也有可能发送一个RST报文段而不是F IN来中途关闭一个连接.这称为 ...