leetcode--Reverse Linked List

来源:互联网 发布:hishop销客多源码 编辑:程序博客网 时间:2024/06/05 10:33

Reverse a singly linked list.

[java] view plain copy
  1. /** 
  2.  * Definition for singly-linked list. 
  3.  * public class ListNode { 
  4.  *     int val; 
  5.  *     ListNode next; 
  6.  *     ListNode(int x) { val = x; } 
  7.  * } 
  8.  */  
  9. public class Solution {  
  10.     public ListNode reverseList(ListNode head) {  
  11.         ListNode h = new ListNode(0);  
  12.         while(head!=null){  
  13.             ListNode n = head.next;  
  14.             head.next = h.next;  
  15.             h.next = head;  
  16.             head = n;  
  17.         }  
  18.         return h.next;  
  19.     }  
  20. }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46388327

原创粉丝点击