反转链表 (Reverse Linked List)

 

思路:

// @Title: 反转链表 (Reverse Linked List)
// @Author: qisiii
// @Date: 2020-06-01 18:16:41
// @Runtime: 0 ms
// @Memory: 38.1 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null) return head;
            ListNode cur=head.next;
            ListNode next;
            head.next=null;
            while(cur!=null){
            next=cur.next;
            cur.next=head;
            head=cur;
            cur=next;
            }
            
            return head;
    }
}

+++ title = “反转链表 (Reverse Linked List)” draft = false +++

思路:

// @Title: 反转链表 (Reverse Linked List)
// @Author: qisiii
// @Date: 2024-04-13 21:25:48
// @Runtime: 0 ms
// @Memory: 41.4 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode pre=null,next=null;
        while(head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;
        }
        return pre;
    }
}

+++ title = “反转链表 (Reverse Linked List)” draft = false +++

思路:

// @Title: 反转链表 (Reverse Linked List)
// @Author: qisiii
// @Date: 2022-03-10 22:10:01
// @Runtime: 0 ms
// @Memory: 41.5 MB
// @comment: 
// @flag: 
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null) return head;
            ListNode cur=head.next;
            ListNode next;
            head.next=null;
            while(cur!=null){
            next=cur.next;
            cur.next=head;
            head=cur;
            cur=next;
            }
            
            return head;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18