-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode problem 19 Remove Nth node from end of the list.txt
74 lines (52 loc) · 1.64 KB
/
Leetcode problem 19 Remove Nth node from end of the list.txt
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
LeetCode Problem 19: Remove Nth Node from the end of Linked List
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
HINT TO SOLVE: two pointer solution. Rabbit and turtle method.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode RemoveNthFromEnd(ListNode head, int n)
{
ListNode leftPointer = new ListNode(0);
leftPointer.next = head;
ListNode rightPointer = null;
ListNode temp = head;
for(int i = 0; i<n-1; ++i)
{
temp = temp.next;
}
rightPointer = temp;
Console.WriteLine("Left: "+leftPointer.val +" "+ "RightValue "+ rightPointer.val);
while(rightPointer.next != null)
{
leftPointer = leftPointer.next;
rightPointer = rightPointer.next;
}
Console.WriteLine("Left: "+ leftPointer.val +" RightValue: "+ rightPointer.val);
if(rightPointer == head)
{
head = null;
}
else if(leftPointer.next == head)
{
head = head.next;
}
else
{
leftPointer.next = leftPointer.next.next;
}
return head;
}
}