0
点赞
收藏
分享

微信扫一扫

Java反转链表

肉肉七七 2022-02-07 阅读 64

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

 

 public class ListNode {

 int val;

 ListNode next;

ListNode(int x) { val = x; }

 }

class Solution {

    public ListNode reverseList(ListNode head) {

        ListNode node=head,pre=null;

        while(node!=null){

           ListNode tmp = node.next;

           node.next=pre;

           pre=node;

           node=tmp;

        }

        return pre;

    }

}

举报

相关推荐

0 条评论