-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path206.Reverse_Linked_List.cpp
More file actions
39 lines (33 loc) · 1.12 KB
/
Copy path206.Reverse_Linked_List.cpp
File metadata and controls
39 lines (33 loc) · 1.12 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Solution for Reverse Linked List contributed by nikki9119.
/*
Problem URL: https://leetcode.com/problems/reverse-linked-list/
Given the head of a singly linked list, reverse the list, and return the reversed list.
Example:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
*/
#include<bits/stdc++.h>
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *current=head;
ListNode *prev=NULL,*next=NULL;
while(current!=NULL) // traverse the list until the end.
{
next=current->next; // next node is the current's next node.
current->next=prev; // point current's next to the previous node.
prev=current; // current node now becomes the previous node,
current=next; // and the next node now becomes the current node.
}
head=prev; // the tail node now becomes the head node.
return head;
}
};
// Time complexity: O(n), where n is the length of the list.