Reverse Linked List

Monisha Mathew
1 min readJun 3, 2019

--

Question: Reverse a singly linked list.

You may view the full question here.

Approach 1: Let’s dive straight into the code —

Approach 1:
//Runtime: 0ms
//Memory usage: 36.5MB
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode currNode = head;
ListNode newNode = null;
while(currNode!=null){
ListNode node = new ListNode(currNode.val);
node.next = newNode;
newNode = node;
currNode = currNode.next;
}
return newNode;
}
}

Find more posts here.

Cheers & Chao!

--

--