- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathReverseLinkedList.java
48 lines (38 loc) · 975 Bytes
/
ReverseLinkedList.java
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
40
41
42
43
44
45
46
47
48
/*
LEETCODE QUESTION : 206. Reverse Linked List
=> Given the head of a singly linked list, reverse the list, and return the reversed list.
*/
// * Definition for singly-linked list.
publicclassListNode {
intval;
ListNodenext;
ListNode() {
}
ListNode(intval) {
this.val = val;
}
ListNode(intval, ListNodenext) {
this.val = val;
this.next = next;
}
}
publicclassReverseLinkedList {
//Submit in leetcode from here :-
publicListNodereverseList(ListNodehead) {
if (head == null) {
returnhead;
}
ListNodeprev = null;
ListNodepresent = head;
ListNodenextnode = present.next;
while (present != null) {
present.next = prev;
prev = present;
present = nextnode;
if (nextnode != null) {
nextnode = nextnode.next;
}
}
returnprev;
}
}