码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 206 Reverse Linked List

时间:2015-06-17 19:46:18      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Reverse链表。使用三个指针head,a,b,最后head会指向最后一个node,故直接返回head。

x -> x -> x -> x =>  x <- x   x -> x   =>   x <- x x -> x  =>      x <- x <- x  x

h     a      b          a.next = head            h  a     b      a.next = head

def reverse(head)
  return if not head 
    a = head.next
    head.next = nil
  while a
    b = a.next
    a.next = head
    head = a
    a = b
  end
  head
end

 

Leetcode 206 Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4584022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!