Posts Tagged 【list】Reverse Linked List

来源:互联网 发布:hg的gp03d石斛兰淘宝 编辑:程序博客网 时间:2024/05/17 06:27

Reverse Linked List

 Total Accepted: 4127 Total Submissions: 11587My Submissions

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) {        if(head == null) return null;        ListNode q = head;        ListNode p = head.next;        while(p != null) {            ListNode temp = p.next;            p.next = q;            q = p;            p = temp;        }        head.next = null;        head = q;        return head;    }}



Have you met this question in a real interview?
0 0