3.反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

示例1

输入

复制

{1,2,3}

返回值

复制

{3,2,1}

public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
       if(head==null)
      {
          return null;
      } 
      ListNode  prew=null;
      while(head!=null)
      {
         ListNode tmp=head.next;
         head.next=prew;
         prew=head;
          head=tmp;
      }
        return prew;
    }
   
    }

猜你喜欢

转载自blog.csdn.net/qq_40408443/article/details/116090758