Flashcards
Reverse Singly linked list
Cards in this deck (2)
- 1Reverse a singly linked list iterative solution/**
* 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;
ListNode prev = null;
ListNode next;
while(cur != null){
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
} - 2Reverse singly linked list using recursionpuclic void ReverseList(ListNode p){
if(p== null)
return p;
ReverseList(p.next);
ListNode q = p.next;
q.next = p;
p.next = null;
}
Information
- Description
- Flashcards on Reverse Singly linked list, created by Suhas S on 12/02/2018.
- Created by
- Suhas S
- Views
- 10
- Tags
- Rating