Home [Easy] Reverse Linked List
Post
Cancel

[Easy] Reverse Linked List

‘Reverse Linked List’ in LeetCode (Easy)


📌 Problem

https://leetcode.com/problems/reverse-linked-list/

📌 Answer

linkednode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * 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) {
        if (head == null || head.next == null) return head;
        ListNode curr = head;
        ListNode prev = null;
        ListNode next;
        while (curr != null) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }
}
This post is licensed under CC BY 4.0 by the author.

[Easy] Linked List Cycle

[Easy] Remove Linked List Elements

Comments powered by Disqus.