avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-206-Reverse Linked List
文章

Leetcode-206-Reverse Linked List

Posted 2022-07-15
4~5 min read

Reverse Linked List

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

Description:

Given the head of a singly linked list, reverse the list, and return the reversed list.

Idea:

**solution **

Step1: set three index, the first one is used to traverse the list and the second one is used to record result, the third one is used to store the next node in the loop as temporary node.

Step2: when the node of traversal is not null, use the temporary node to store the next node of current node(so that can continue traverse). make the next node of current node points to the previous node, so that reverse the side of linked list.

make the previous node point to current node to record result. make the current node point to temporary node to continue traversal.

Code:

/**
 * 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) {
        //set the index to traverse;
        ListNode current = head;
        //set  the previous node  to record result
        ListNode previous = null;
        //set the temp node to store the original next node
        ListNode temp = null;
        //until the end
        while(current != null){
            //temp to record the next node in the loop
            temp = current.next;
            //reverse the side of node
            current.next = previous;
            previous = current;
            //make the index point to temp to continue traverse
            current = temp;
        }
        return previous;
    }
}
Leetcode
Leetcode Linked List
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

Leeetcode-707-Design Linked List

NEWER

Leetcode-24-Swap Nodes in Pairs

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents