1500802012 朋毛东知

翻转链表 代码代码:
/**

  • Definition of ListNode
  • class ListNode {
  • public:
  • int val;
    
  • ListNode *next;
    
  • ListNode(int val) {
    
  •     this->val = val;
    
  •     this->next = NULL;
    
  • }
    
  • }
    /
    class Solution {
    public:
    /
    *
    • @param head: The first node of linked list.
    • @return: The new head of reversed linked list.
      */
      ListNode *reverse(ListNode *head) {
      // write your code here
      ListNode *prev=NULL;
      while(head!=NULL)
      {
      ListNode *temp=head->next;
      head->next=prev;
      prev=head;
      head=temp;
      }
      return prev;
      }
      };

posted on 2017-08-17 22:15  p666  阅读(78)  评论(0编辑  收藏  举报

导航