Skip to content

Commit 1539c1f

Browse files
authored
Merge pull request larissalages#48 from catw101/master
Added leetcode problem and solution: Reverse Linked List in Java.
2 parents 8c253c3 + 42eef1f commit 1539c1f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

leetcode/java/linkedlist/206.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode reverseList(ListNode head) {
13+
ListNode prev = null;
14+
ListNode cur = head;
15+
ListNode nxt = null;
16+
while (cur != null) {
17+
nxt = cur.next;
18+
cur.next = prev;
19+
prev = cur;
20+
cur = nxt;
21+
}
22+
return prev;
23+
}
24+
}

0 commit comments

Comments
 (0)