206.|206. Reverse Linked List
文章图片
Java
/**
* Definition for singly-linked list.
* public class ListNode {
*int val;
*ListNode next;
*ListNode(int x) { val = x;
}
* }
*/
public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return head;
ListNode pre=null;
ListNode cur=head;
ListNode nt=head.next;
while(cur.next!=null)
{
nt=cur.next;
cur.next=pre;
pre=cur;
cur=nt;
}
cur.next=pre;
head=cur;
return head;
}
}
Javascript
/**
* Definition for singly-linked list.
* function ListNode(val) {
*this.val = val;
*this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
if(head===null) return head;
var pre=null;
var cur=head;
var nt=head.next;
while(cur.next!==null)
{
nt=cur.next;
cur.next=pre;
pre=cur;
cur=nt;
}
cur.next=pre;
head=cur;
return head;
};
优解,差不多 【206.|206. Reverse Linked List】注意头节点的指针要指向空
推荐阅读
- 206.6.11
- redis入门2
- LinkedBlockingQueue分析
- Java|Java JUC ConcurrentLinkedQueue解析
- leetcode:Reverse Linked List II
- 攻防世界|攻防世界 reverse (一)
- 【逆向笔记】2017年全国大学生信息安全竞赛|【逆向笔记】2017年全国大学生信息安全竞赛 Reverse 填数游戏
- 01.翻转字符串
- CTF论剑场Web1
- 翻转树