Reverse Linked List

来源:互联网 发布:大数据工程师工作职责 编辑:程序博客网 时间:2024/06/06 14:08

题目

Reverse a singly linked list.

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) {        ListNode pre = null;        ListNode next = null;        ListNode cur = head;        if(head!=null){            while(cur.next!=null){                next = cur.next;                cur.next = pre;                pre =cur;                cur = next;            }            cur.next =pre;        }        return cur;    }}
0 0
原创粉丝点击