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 ...
随机推荐
- PHP->利用GD库新建图像
1.确认php中GD库是否开启 在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前边的(分号) ';' 即可,一般php是默认开启的 2.绘画步骤 创建一个画布(画 ...
- rabbitmq之消息转储vm_memory_high_watermark_paging
rabbitmq为了预防内存达到上限,会在内存使用到了一定比例后,将消息转储到磁盘去.
- MongoDB安装并设置为windows服务以使其开机自启
在MongoDB的官方下载windows平台的压缩zip文件,地址:https://www.mongodb.org/dr/fastdl.mongodb.org/win32/mongodb-win32- ...
- php判断闰年
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Class类
package com.imooc.reflect; public class ClassDemo1 { public static void main(String[] args) { //Foo的 ...
- 传入一个label或者button,传入5s,6和6+的文字尺寸 快速定义文字大小
func isIphone6() -> Bool { { return true } else { return false } } func isIphone6Plus() -> Boo ...
- 使用curl命令操作elasticsearch
使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...
- Java 生成验证码
package com.lf.testvity; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; im ...
- function 类型
一.函数的声明方式 1.普通的函数声明 function box(num1,num2){ return num1+num2; }alert(box(1,2)); 2.使用变量初始化函数 var box ...
- Ubuntu文件操作命令
1.工具栏放下面或者左边的命令 gsettings set com.canonical.Unity.Launcher launcher-position Left gsettings set com. ...